HTML DOM Input Password 对象
htmlweb developmentfront end technology更新于 2025/10/13 13:00:17
HTML DOM Input Password 对象与类型为"password"的 <input> 元素关联。我们可以分别使用 createElement() 和 getElementById() 方法创建和访问类型为 password 的输入元素。
属性
以下是 Input Password 的属性 −
| Sr.No | 属性 &说明 |
|---|---|
| 1 | autocomplete 设置或返回密码字段的 autocomplete 属性值 |
| 2 | autofocus 设置或返回密码字段在页面加载时是否自动获得焦点。 |
| 3 | defaultValue 设置或返回密码字段的默认值。 |
| 4 | disabled 设置或返回密码字段是否被禁用。 |
| 5 | form 返回包含密码的表单的引用密码字段 |
| 6 | maxLength 设置或返回密码字段的maxlength属性值。 |
| 7 | name 设置或返回密码字段的name属性值。 |
| 8 | pattern 设置或返回密码字段的pattern属性值。 |
| 9 | placeholder 设置或返回密码字段的placeholder属性值。 |
| 10 | readOnly 设置或返回密码字段是否为只读,或不 |
| 11 | required 设置或返回在提交表单前是否必须填写密码字段。 |
| 12 | size 设置或返回密码字段的 size 属性值。 |
| 13 | type 这是一个只读属性,用于返回密码字段所属的表单元素类型。 |
| 14 | value 设置或返回密码字段的 value 属性值。 |
方法
以下是密码对象 − 的方法
| Sr.No | 方法与说明 |
|---|---|
| 1 | select() 选择密码字段内容。 |
语法
以下是语法 −
创建输入密码对象 −
var P = document.createElement("INPUT");
P.setAttribute("type", "password");
Let us look at an example for the Input Password object −
<!DOCTYPE html>
<html>
<head>
<script>
function createPASS() {
var P = document.createElement("INPUT");
P.setAttribute("type", "password");
document.body.appendChild(P);
}
</script>
</head>
<body>
<p>Create an input field with type password by clicking the below button</p>
<button onclick="createPASS()">CREATE</button>
<br><br>
PASSWORD:
</body>
</html>
输出
这将产生以下输出 −

点击"创建"按钮并在新创建的密码字段中输入 −

在上面的例子中 −
我们有一个 CREATE 按钮,当用户点击它时,它会执行 createPass() 方法 −
<button onclick="createPASS()>>CREATE</button>
createPass() 方法使用 document 对象的 createElement() 方法,通过传递 “INPUT” 来创建 <input> 元素。作为参数。新创建的输入元素被赋值给变量 P,并使用 setAttribute() 方法创建一个值为 password 的属性。记住,setAttribute() 会创建该属性,如果该属性之前不存在,则会赋值。最后,在文档主体上使用 appendChild() 方法,我们将类型为 password 的输入元素作为主体的子元素附加。 −
function createPASS() {
var P = document.createElement("INPUT");
P.setAttribute("type", "password");
document.body.appendChild(P);
}
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

