HTML DOM Input Password size 属性

htmlweb developmentfront end technology更新于 2025/10/13 18:52:17

HTML DOM Input Password size 属性用于设置或返回输入密码 size 属性值。该属性用于定义密码字段的字符宽度。默认宽度为 20 个字符。

语法

以下是语法 −

设置密码 size 属性 −

passwordObject.size = number

此处,number 表示密码字段的字符宽度。默认宽度为 20 个字符。

示例

让我们看一个输入密码 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>

输出

这将产生以下输出 −

点击"更改"按钮 −

在上面的例子中 −

我们首先创建了一个 ID 为"PASS1"的密码输入字段。

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

然后,我们创建了一个按钮"CHANGE",当用户点击"减号"时,它将执行 changeSize() 方法。

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

changeSize() 方法使用 getElementById() 方法获取类型为 password 的输入字段,并将其添加到其 size 属性。密码字段的总宽度现在增加了 20 个字符。然后,我们再次使用 size 属性获取大小并将其赋值给变量 s。新的大小值随后会通过其 innerHTML 属性显示在 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;
}

相关文章


有用资源