HTML DOM Input Reset autofocus 属性

htmlweb developmentfront end technology更新于 2025/10/16 23:40:17

HTML DOM Input Reset autofocus 属性与 HTML <input> 元素的 autofocus 属性相关联。此属性用于设置或返回重置按钮在页面加载时是否自动对焦。

语法

以下是语法 −

设置自动对焦属性 −

resetObject.autofocus = true|false

此处,true 表示重置按钮应获得焦点,false 表示否则。默认设置为 false。

示例

让我们看一个 Input Reset autofocus 属性的示例 −

<!DOCTYPE html>
<html>
<body>
<h1> Reset Autofocus property</h1>
<form style="border:solid 2px green;padding:2px">
UserName: <input type="text" id="USR"> <br>
Location: <input type="text" id="Age"> <br><br>
<input type="reset" id="RESET1" autofocus>
</form>
<p>Get the reset button autofocus property value by clicking the below button</p>
<button type="button()" onclick="FocusVal()">CHECK</button>
<p id="Sample"></p>
<script>
   function FocusVal(){
      var f = document.getElementById("RESET1").autofocus;
      document.getElementById("Sample").innerHTML = "The reset button autofocus property is set to: "+f;
   }
</script>
</body>
</html>

输出

这将产生以下输出 −

点击"检查"按钮后 −

在上面的例子中 −

我们创建了一个 <input> 元素,其 type=”reset”,id=”RESET1”。点击此按钮将重置表单数据。此按钮位于一个包含两个文本字段的表单内,并且该表单也应用了内联样式 −

<form style="border:solid 2px green;padding:2px">
UserName: <input type="text" id="USR"> <br>
Location: <input type="text" id="Age"> <br><br>
<input type="reset" id="RESET1">
</form>

然后我们创建了一个按钮 CHECK,当用户点击该按钮时,它将执行 FocusVal() 方法 −

<button type="button()" onclick="FocusVal()">CHECK</button>

FocusVal() 方法使用 getElementById() 方法获取类型为 reset 的输入元素,并获取其 autofocus 属性。autofocus 属性根据重置按钮的 autofocus 属性值返回 true 或 false。该值被赋值给变量 f,并使用其 innerHTML 属性显示在 id 为"Sample"的段落中。 −

function FocusVal(){
   var f = document.getElementById("RESET1").autofocus;
   document.getElementById("Sample").innerHTML = "The reset button autofocus property is set to: "+f;
}

相关文章


有用资源