代码描述:setTimeout()设置 延迟时间为0毫秒。setTimeout()设置延迟时间为0毫秒代码下载
关于setTimeout()函数的基本用法这里就不多介绍了。 具体可以参阅setTimeout()函数用法详解一章节。 也就是说此函数可以设置指定函数的执行的延迟时间,但是在实际应用中,setTimeout()函数的延迟时间设置0,感觉这毫无道理。 看下面的代码实例:
<script> obt.onmousedown=function(){ setTimeout(function(){ document.getElementById('txt').focus(); },0); }; </script>
是不是感觉上面的代码毫无道理,为何不直接调用函数,而非要将其放入setTimeout()函数中,并且将其设置为0秒以后执行。下面来看一段代码实例:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="https://www.51qianduan.com/" /> <title>51前端</title> <script type="text/javascript" > function get(id){ return document.getElementById(id); } window.onload=function(){ get('bt').onmousedown=function(){ var input = document.createElement('input'); input.setAttribute('type','text'); input.setAttribute('value','softwhy.com'); get('box').appendChild(input); input.focus(); input.select(); } } </script> </head> <body> <button id="bt">创建文本框</button> <p id="box"></p> </body> </html>
在上面的代码中,鼠标按下按钮可以创建一个文本框,但是文本框中的内容不会被选中。 说明:这里之所以使用onmousedown事件,是因为此事件内部处理可能会导致文本框无法被选中,如果使用其他时间就会被选中。 代码修改如下:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="https://www.51qianduan.com/" /> <title>51前端</title> <script type="text/javascript" > function get(id){ return document.getElementById(id); } window.onload=function(){ get('bt').onmousedown=function(){ var input = document.createElement('input'); input.setAttribute('type','text'); input.setAttribute('value','softwhy.com'); get('box').appendChild(input); setTimeout(function(){ input.focus(); input.select(); },0); } } </script> </head> <body> <button id="bt">创建文本框</button> <p id="box"></p> </body> </html>
请绑定手机号,在继续操作
注意:只有绑定手机以后,才可使用网站全部功能