我有三个输入字段,它们都是密码字段,我想做的是在输入字段中放置 fontawesome
图标,点击它可以显示或隐藏密码
目前我有这个但是有按钮,它只对一个字段而不是所有三个字段执行,我必须为所有三个调用 3 个不同的函数还是可以用相同的函数完成
片段
function show() {
var a = document.getElementById("confirm_password");
var b = document.getElementById("display2");
if (a.type == "password") {
a.type = "text";
} else {
a.type = "password";
}
}
<div class="col-lg-3">
<h5 id="commonHeader">Current Password</h5>
<input type="password" id="currentPass" name="currentpass" class="commanClass" placeholder="Current Password">
<button type="button" onclick="show()" id="display"></button>
</div>
<div class="col-lg-3">
<h5 id="commonHeader">New Password</h5>
<input type="password" id="newPass" name="newpass" required="required" class="commanClass" placeholder="Current Password">
<button type="button" onclick="show()" id="display1"></button>
</div>
<div class="col-lg-3">
<h5 id="commonHeader">Confirm Password</h5>
<input type="password" id="confirm_password" required="required" placeholder="Confirm Password">
<button type="button" onclick="show()" id="display2"></button>
</div>
<div>
<hr>
<br>
<button type="submit" id="save" class="commonButton">Save</button>
<button type="button" id="save" class="commonButton">Clear</button>
</div>
在我的代码片段中,我有 3 个输入字段,我必须为所有输入字段提供一个图标,如果用户单击该图标,它将显示当前字段的密码
目前只适用于一个领域
最佳答案
您可以使用 removeAttribute 删除 type="password"并同时使用 setAttribute 将类型设置为文本
function show(a) {
var x=document.getElementById(a);
var c=x.nextElementSibling
if (x.getAttribute('type') == "password") {
c.removeAttribute("class");
c.setAttribute("class","fas fa-eye");
x.removeAttribute("type");
x.setAttribute("type","text");
} else {
x.removeAttribute("type");
x.setAttribute('type','password');
c.removeAttribute("class");
c.setAttribute("class","fas fa-eye-slash");
}
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<div class="col-lg-3">
<h5 id="commonHeader">Current Password</h5>
<input type="password" id="currentPass" name="currentpass" class="commanClass" placeholder="Current Password">
<i onclick="show('currentPass')" class="fas fa-eye-slash" id="display"></i>
</div>
<div class="col-lg-3">
<h5 id="commonHeader">New Password</h5>
<input type="password" id="newPass" name="newpass" required="required" class="commanClass" placeholder="Current Password">
<i onclick="show('newPass')" class="fas fa-eye-slash" id="display"></i>
</div>
<div class="col-lg-3">
<h5 id="commonHeader">Confirm Password</h5>
<input type="password" id="confirm_password" required="required" placeholder="Confirm Password">
<i onclick="show('confirm_password')" class="fas fa-eye-slash" id="display"></i>
</div>
<div>
<hr>
<br>
<button type="submit" id="save" class="commonButton">Save</button>
<button type="button" id="save" class="commonButton">Clear</button>
</div>
关于javascript - 如何设置点击图标时可见的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54528416/