根据我的标题,如何在密码字段中实现onfocus
和onblur
?默认值为密码(可读),当我们单击密码字段时,它将返回密码格式。
举个例子,我不想看到“●●●●●●●●”,而是“密码”
<input type="password" name="password" value="password" onfocus="this.value=(this.value=='password') ? '' : this.value;" onblur="this.value=(this.value=='') ? 'password' : this.value;" />
更新
我找到的 Google 结果 Change Input Type Dynamically但代码太长了。也许 jquery 可以让它变得更短。
最佳答案
如果您尝试使用 1 个字段,您将在 Internet Explorer 中遇到麻烦,因为 IE 将不允许让您更改字段的类型
。
您需要使用 2 个字段,当文本输入获得焦点时,隐藏它,显示密码输入,然后获得焦点。
你需要这样的东西(使用 jQuery):
<input type="text" id="fakeemail" value="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="22474f434b4e62464d4f434b4c0c414d4f" rel="noreferrer noopener nofollow">[email protected]</a>"/>
<input type="password" id="realemail" value="" name="email" style="display:none;"/>
$('#fakeemail').focus(function(){
$(this).hide();
$('#realemail').show().focus();
});
$('#realemail').blur(function(){
if(this.value == ''){
$(this).hide();
$('#fakeemail').show();
}
});
关于javascript - 如何为密码字段实现 Javascript onfocus() 和 onblur()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3275469/