在html中,focus是“焦點”的意思,focus()方法用于為元素設置焦點,語法為“HTMLElementObject.focus()”;元素通過focus()方法設置的焦點,可以通過blur()方法進行移除。
本教程操作環境:windows10系統、html5&&javascript1.8.5版、Dell G3電腦。
html中focus的用法是什么
focus() 方法用于為元素設置焦點(如果可以設置)。
語法為:
HTMLElementObject.focus()
示例如下:
<html> <head> <meta charset="utf-8"> <title>123</title> </head> <body> <input type="text" id="myText" value="文本域"> <p>點擊按鈕設置或移除以上文本域的焦點。</p> <input type="button" onclick="getfocus()" value="獲取焦點"> <input type="button" onclick="losefocus()" value="移除焦點"> <script> function getfocus() { document.getElementById("myText").focus(); } function losefocus() { document.getElementById("myText").blur(); } </script> </body> </html>
輸出結果:
推薦教程:《html視頻教程》