HTML DOM Input Password type 属性

htmlweb developmentfront end technology更新于 2025/10/13 20:28:17

HTML DOM Input Password type 属性与 type="password"的输入元素关联。它将始终返回输入密码元素的密码。

语法

以下是密码类型属性的语法 −

passwordObject.type

示例

让我们来看一个输入密码类型属性的示例 −

<!DOCTYPE html>
<html>
<body>
<h1>password type property</h1>
PASSWORD: <input type="password" id="PASS1">
<p>Get the above element type by clicking the below button</p>
<button onclick="getType()">Get Type</button>
<p id="Sample"></p>
<script>
   function getType() {
      var t = document.getElementById("PASS1").type;
      document.getElementById("Sample").innerHTML = "The type for the input field is : "+t;
   }
</script>
</body>
</html>

输出

这将产生以下输出 −

点击"获取类型"按钮 −

在上面的例子中 −

我们创建了一个类型为 password 的输入字段,其 id 设置为"PASS1"。

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

然后我们创建了"获取类型"按钮,当用户点击时将执行 getType() 方法 −

<button onclick="getType()>获取类型</button>

getType() 方法使用 getElementById() 方法获取输入元素,并将其 type 属性值赋给变量 t。然后,该变量将使用其 innerHTML 属性显示在 id 为"Sample"的段落中。 −

function getType() {
   var t = document.getElementById("PASS1").type;
   document.getElementById("Sample").innerHTML = "The type for the input field is : "+t;
}

相关文章


有用资源