JavaScript focus方法用于为html元素提供焦点。它将元素设置为当前文档中的活动元素。它可以一次应用于当前文档中的一个html元素。元素可以是按钮、文本字段或窗口等。所有浏览器都支持该元素。 语法:
null
HTMLElementObject.focus()
参数: 它不接受任何参数。 返回值: 此方法不返回任何值。
<html> <head> <script type= "text/javascript" > function myFunction() { document.getElementById( "focus" ).focus(); } </script> </head> <body> <form action= "#" > <br> <br> <label> Hover me: </label> <input type= "text" onmousemove=myFunction() id= "focus" > <!-- onmousemove is an event which occurs when somenone hovers the mouse on that particular element and calls the function of javascript --> <br> <br> <label>Without Focus: </label> <input type= "text" > <br> <br> <input type= "button" value= "submit" > </form> </body> </html> |
输出: 焦点字段可以在 模糊() 方法。 语法:
HTMLElementObject.blur()
参数: 此方法不接受任何参数。 单击字段时使用模糊方法的图示: 代码:
<html> <head> <script type= "text/javascript" > function setFocus() { document.getElementById( "focus" ).focus(); } function removeFocus() { document.getElementById( "focus" ).blur(); } </script> </head> <body> <input type= "button" onclick= "setFocus()" value= "set focus" > <input type= "button" onclick= "removeFocus()" value= "remove focus" > <br> <br> <input type= "text" id= "focus" > </body> </html> |
输出: 点击设置焦点后- 点击移除焦点后-
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END