在html中,focus是“焦點”的意思,focus()方法用于為元素設(shè)置焦點,語法為“HTMLElementObject.focus()”;元素通過focus()方法設(shè)置的焦點,可以通過blur()方法進行移除。
本教程操作環(huán)境:windows10系統(tǒng)、html5&&javascript1.8.5版、Dell G3電腦。
html中focus的用法是什么
focus() 方法用于為元素設(shè)置焦點(如果可以設(shè)置)。
語法為:
HTMLElementObject.focus()
示例如下:
<html> <head> <meta charset="utf-8"> <title>123</title> </head> <body> <input type="text" id="myText" value="文本域"> <p>點擊按鈕設(shè)置或移除以上文本域的焦點。</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>
輸出結(jié)果:
推薦教程:《html視頻教程》