輸入框 得到光標的這個行爲叫作獲取焦點 失去光標的這個行爲叫作失去焦點 blur 失去焦點 一、獲取標籤的時候,必定要先等頁面加載完成,再去獲取這個標籤。 能夠將整個script代碼寫在body的下面。 二、<script>標籤按照規範,應該放在head標籤中 window.onload = function(){ }
方法一:html
<body> <input type="text" id = 'input1'> </body> <script> var oInput = document.getElementById('input1'); alert(oInput); </script> </html>
方法二;函數
<script> window.onload = function(){ /*這裏寫js代碼*/ } </script>
以下:spa
<script> window.onload = function(){ //這個函數是在頁面加載完成之後執行的一個函數 var oInput = document.getElementById('input1'); //添加,失去焦點時候,執行的代碼 oInput.onblur = function(){ // alert(1); //表單驗證,發生在這裏。 } } </script> </head> <body> <input type="text" id = 'input1'> <h1>首頁</h1> </body>