我希望能够使用纯Javascript创建一个密码切换功能,当单击眼睛图标时使用。我已经编写了相应的代码,但它只能显示密码文本而无法反向操作。请问下面的代码存在哪些逻辑错误。
function show() {
var p = document.getElementById('pwd');
p.setAttribute('type', 'text');
}
function hide() {
var p = document.getElementById('pwd');
p.setAttribute('type', 'password');
}
function showHide() {
var pwShown = 0;
document.getElementById("eye").addEventListener("click", function() {
if (pwShown == 0) {
pwShown = 1;
show();
} else {
pwShow = 0;
hide();
}
}, false);
}
<input type="password" placeholder="Password" id="pwd" class="masked" name="password" />
<button type="button" onclick="showHide()" id="eye">
<img src="eye.png" alt="eye"/>
</button>
pwShown=!pwShown
- mplungjan