PHP前端开发

HTML DOM Input Password autofocus 属性

百变鹏仔 3个月前 (09-22) #HTML
文章标签 属性

html dom input password autofocus属性与html 元素的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;}