【发布时间】:2015-06-21 23:22:33
【问题描述】:
我正在尝试制作一个按钮来显示输入上写的密码。 我可怜的 JS 技能告诉我,我可以通过这样做来实现它:
我试过了:
$('#passReveal').on('click', function(){
$( "#input-show" ).attr('type', 'text');
if ($( "#input-show" ).attr('type', 'text')){
$( "#input-show" ).attr('type', 'password');
}
});
但不起作用。
我认为这是最接近的,但我有一个错误:您必须单击两次按钮才能使其第一次工作,这是为什么呢?
$('#passReveal').on('click', function(){
$( "#input-show" ).attr('type', 'text');
$( "#input-show" ).toggleClass('passRevealed');
if ($( "#input-show" ).hasClass('passRevealed')){
$( "#input-show" ).attr('type', 'password');
}
});
https://jsfiddle.net/tepLkc7u/2/
我希望你能理解我糟糕的英语,我正在学习:)
【问题讨论】:
-
你的逻辑正好相反。你使用的是
.attr('type', 'passowrd'),而它应该是.attr('type', 'text')