输入框 得到光标的这个行为叫作获取焦点 失去光标的这个行为叫作失去焦点 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>