我正在尝试编写一个将多个密码字段显示为文本的代码。这是我采取的方法,我的重点是使代码尽可能短,但我采取的方法似乎做错了。
方法一:
<input type="password" class="password">
<input type="password" class="password">
<input type="checkbox" onchange="document.getElementByClassName('.password').type = this.checked ? 'text' : 'password'"> Show password
Run Code Online (Sandbox Code Playgroud)
getElementById如果我使用但不使用上述方法,则效果很好getElementByClassName
方法2: 小提琴
Password: <input type="password" value="FakePSW" id="myInput"><br><br>
<input type="checkbox" onclick="myFunction()">Show Password
<script>
function myFunction() {
var x = document.getElementById("myInput");
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
如果使用此方法,也会出现同样的问题getElementByClassName。
javascript ×1