HTML DOM Input Reset 物件


HTML DOM Input Reset 物件與型別為“reset”的 <input> 元素相關聯。我們可以分別使用 createElement() 和 getElementById() 方法來建立和訪問型別為 reset 的輸入元素。

屬性

以下是 Input reset 物件的屬性:

屬性描述
autofocus設定或返回重置按鈕在頁面載入時是否應自動獲得焦點。
defaultValue設定或返回重置按鈕的預設值。
disabled設定或返回重置按鈕是否已被停用。
form返回包含重置按鈕的表單的引用。
name設定或返回重置按鈕的 name 屬性值。
type返回重置按鈕的表單元素型別。
value設定或返回滑塊控制元件的 value 屬性值。

示例

讓我們來看一個 Input Reset 物件的示例:

<!DOCTYPE html>
<html>
<head>
<script>
   function resetCreate() {
      var Res = document.createElement("INPUT");
      Res.setAttribute("type", "reset");
      document.body.appendChild(Res);
   }
</script>
</head>
<body>
<h1>Input Reset object</h1>
<p>Create an input field with type reset by clicking the below button</p>
<button onclick="resetCreate()">CREATE</button>
<br><br>
</body>
</html>

輸出

這將產生以下輸出:

點選“建立”按鈕時:

在上面的示例中:

我們建立了一個名為“建立”的按鈕,當用戶點選時,它將執行 resetCreate() 方法。

<button onclick="resetCreate()">CREATE</button>

resetCreate() 方法使用 document 物件的 createElement() 方法透過傳遞“INPUT”作為引數來建立 <input> 元素。新建立的輸入元素被分配給變數 Res,並且使用 setAttribute() 方法,我們建立了一個名為 type 的屬性,其值為 reset。

請記住,如果屬性之前不存在,setAttribute() 會先建立屬性,然後賦值。最後,使用 document body 上的 appendChild() 方法,我們將型別為 reset 的輸入元素作為 body 的子元素附加。

function resetCreate() {
   var Res = document.createElement("INPUT");
   Res.setAttribute("type", "reset");
   document.body.appendChild(Res);
}

更新於: 2019年8月19日

143 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告