目录

如何 - 切换密码可见性


使用 JavaScript 在密码可见性之间切换。


密码:

显示密码


切换密码可见性

步骤1)添加HTML:

示例

<!-- Password field -->
Password: <input type="password" value="FakePSW" id="myInput">

<!-- An element to toggle between password visibility -->
<input type="checkbox" onclick="myFunction()">Show Password

步骤 2) 添加 JavaScript:

示例

function myFunction() {
  var x = document.getElementById("myInput");
  if (x.type === "password") {
    x.type = "text";
  } else {
    x.type = "password";
  }
}
亲自试一试 »