HTML DOM input Password autofocus属性与HTML d5fd7aea971a85678ba271703566ebfd元素的autofocus属性相关联。此属性用于设置或返回在页面加载时输入密码字段是否应自动聚焦。
以下是设置autofocus属性的语法:
设置autofocus属性 -
passwordObject.autofocus = true|false
在这里,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>
这将产生以下输出 −
在点击CHECK FOCUS按钮时 −
在上面的示例中 −
我们创建了一个类型为“password”的输入字段,id为“Pass”,并启用了自动对焦属性,即设置为true −
Password: <input type="password" id="PASS" autofocus>
我们接下来创建了一个按钮 CHECK FOCUS,当用户点击时将执行 FocusVal() 方法 −
<button onclick="FocusVal()">CHECK FOCUS</button>
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; }
以上是HTML DOM 输入密码 autofocus 属性的详细内容。更多信息请关注PHP中文网其他相关文章!