[英]How to change the HTML input type attribute from 'password' to' text'?
我想更改HTML'input'元素'type'属性,从type =“password”更改为type =“text”。
我知道按ID查找元素会更容易,但并不是每个页面都有相同的密码输入ID。 如何定位密码输入并将其内部值更改为一种文本。
我有几行JavaScript代码:
a=document.getElementsByTagName("input");
a.setAttribute(type,text);
要将password类型的所有元素转换为text:
var arr = document.getElementsByTagName("input");
for (var i = 0; i < arr.length; i++) {
if (arr[i].type == 'password') arr[i].setAttribute('type','text');
}
注意:并非所有浏览器都允许对输入的type
属性进行动态更改。
您可以使用以下查询获取密码字段。
document.querySelector('input[type="password"]').type = "text";
如果您正在处理多个字段,则可以使用querySelectorAll
并循环遍历它们。
var pwds = document.querySelectorAll('input[type="password"]');
for (var i=0;i<pwds.length;i++){
pwds[i].type = "text";
}
这会将类型密码的每个输入转换为文档类型文本的输入。
Array.prototype.slice.call(document.querySelectorAll('input[type="password"]'))
.forEach(function (elt) {
elt.setAttribute('type', 'text');
});
该函数称为document.getElementsByTagName
(带有s),它生成一个元素列表。 因此,您需要使用索引(第一个输入a[0]
等)来解决它。
我认为问题是确定你需要改变哪些输入,如果他们没有标记ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.