HTML DOM Input 重置 autofocus 屬性


HTML DOM Input 重置 autofocus 屬性與 HTML <input> 元素的 autofocus 屬性相關。此屬性用於設定或返回頁面載入時是否應自動聚焦重置按鈕。

語法

以下是語法:

設定 autofocus 屬性:

resetObject.autofocus = true|false

這裡,true 表示重置按鈕應該獲得焦點,false 表示反之。預設情況下設定為 false。

示例

讓我們來看一個 Input 重置 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>

輸出

這將產生以下輸出:

點選“CHECK”按鈕:

在上面的例子中:

我們建立了一個 type=”reset”,id=”RESET1” 的 <input> 元素。點選此按鈕將重置表單資料。此按鈕位於包含兩個文字欄位的表單內,表單也應用了內聯樣式:

<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() 方法獲取 type 為 reset 的 input 元素,並獲取 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;
}

更新於:2019年8月22日

160 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告