HTML DOM Input Password size 屬性


HTML DOM Input Password size 屬性用於設定或返回輸入密碼大小屬性值。它用於定義密碼欄位寬度的字元數。預設寬度為 20 個字元。

語法

以下是語法 −

設定密碼大小屬性 −

passwordObject.size = number

這裡,number 表示密碼欄位的字元寬度。預設寬度為 20 個字元。

示例

我們來看一個有關 Input password size 屬性的示例 −

<!DOCTYPE html>
<html>
<body>
<h1>Input password size property</h1>
Password: <input type="password" id="PASS1">
<p>Change the Password field width by clicking the below button</p>
<button onclick="changeSize()">CHANGE</button>
<p id="Sample">
<script>
   function changeSize() {
      document.getElementById("PASS1").size+=20;
      var s=document.getElementById("PASS1").size;
      document.getElementById("Sample").innerHTML="The password field width is now "+ s;
   }
</script>
</body>
</html>

輸出

這將產生以下輸出 −

單擊 CHANGE 按鈕 −

在上面的示例中 −

我們首先建立了一個帶有 id“PASS1”的輸入密碼欄位。

Password: <input type="password" id="PASS1">

然後我們建立了一個 CHANGE 按鈕,當用戶單擊該按鈕時,該按鈕將執行 changeSize() 方法 −

<button onclick="changeSize()">CHANGE</button>

changeSize() 方法使用 getElementById() 方法來獲取型別為密碼的輸入欄位,並新增到其 size 屬性中。密碼欄位的總寬度現在增加了 20 個字元。然後我們再次使用 size 屬性來獲取大小並將其分配給變數 s。然後使用其 innerHTML 屬性將新的 size 值顯示在 id 為 “Sample” 的段落中。

function changeSize() {
   document.getElementById("PASS1").size+=20;
   var s=document.getElementById("PASS1").size;
   document.getElementById("Sample").innerHTML="The password field width is now "+ s;
}

更新於: 2019-8-22

112 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.