HTML DOM input Password autofocus属性与HTML <input>元素的autofocus属性相关联。此属性用于设置或返回在页面加载时输入密码字段是否应自动聚焦。
语法
以下是设置autofocus属性的语法:
设置autofocus属性 -
在这里,true表示密码字段应该获得焦点,false表示否。默认情况下设置为false。
示例
让我们来看一个关于Input Password autofocus属性的例子 -
<!DOCTYPE html> <html> <body> <h1>Input password autofocus property</h1> Password: <input type="password" id="PASS" autofocus> <br><br> <button onclick="FocusVal()">CHECK FOCUS</button> <p id="Sample"></p> <script> function FocusVal() { var P = document.getElementById("PASS").autofocus; document.getElementById("Sample").innerHTML = "The password field has autofocus property set to "+P; } </script> </body> </html>
The FocusVal() method gets the input element with type password using the getElementById() method and gets its autofocus property. The autofocus property returns true and false depending on the element autofocus attribute value. This value is assigned to variable P and displayed in the paragraph with id “Sample” using its innerHTML property −
function FocusVal() { var P = document.getElementById("PASS").autofocus; document.getElementById("Sample").innerHTML = "The password field has autofocus property set to "+P; }