JS 动态修改 input 的 type 属性
发布时间:2016-01-12, 15:38:11 分类:HTML | 编辑 off 网址 | 辅助
正文 1961字数 450,219阅读
JS 动态修改 input 的 type 属性调用: $(“#uname”);可选参数class为获取焦点时input的class,但是清除密码的时候出了一个问题,用户输入的密码应该是 ”*****”,这里需要将input 的type 属性由 text 换成 password ,如果用户没有输入密码,鼠标失去焦点的时候 type换回 text ,value 值为 “密码”。
发现并没有实现预期效果,出现 uncaught exception type property can’t bechanged 错误,查看jQuery 1.42源码 1488 行
jQuery 修改不了用源生的JS呢?
发现在FF下可以修改并将密码输入框type 修改为 “password” 并将value设置为空,而IE下却提示无法得到type属性,不支持该命令。 弹出 type 看看真的无法得到吗?
发现弹出text ,原来不是无法得到,只是IE下不能修改。 各种 google后发现input的type属性只能在初始的时候设定却不能修改(IE不能,FF可以,个人认为,不知道是否准确,如有不对请大牛指教)。这样我是否可以先remove然后再生成一个type是password的密码输入框呢?
可以实现,但是输入为空鼠标失去焦点再返回到起始状态的时候不怎么好写,节点生成删除为什么不用两个密码框来显示隐藏呢?
html
<input id="showPwd" class="txt" type="text" value="密码" tabindex="2" />
<input id="pwd" class="txt" name="password" type="password" />
Run code
Cut to clipboard
js
var showPwd = $("#showPwd"), pwd = $("#pwd");
showPwd.focus(function(){
pwd.show().focus();
showPwd.hide();
});
pwd.blur(function(){
if(pwd.val()=="") {
showPwd.show();
pwd.hide();
}
});
Run code
Cut to clipboard
原文 http://blog.csdn.net/teresa502/article/details/6546361
Run code
Cut to clipboard
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.9.0/jquery.min.js"></script>
<input id="showPwd" class="txt" type="text" value="密码" tabindex="2" />
<input id="pwd" class="txt" name="password" type="password" value="" style="display:none;" />
<script>
var showPwd = $("#showPwd"), pwd = $("#pwd");
showPwd.focus(function(){
pwd.show().focus();
showPwd.hide();
});
pwd.blur(function(){
if(pwd.val()=="") {
showPwd.show();
pwd.hide();
}
});
</script>
Run code
Cut to clipboard
(支付宝)给作者钱财以资鼓励 (微信)→
有过 1 条评论 »
I am hoping to view the same high-grade content from you in the future as well.
In fact, your creative writing abilities has motivated me to get my own, personal website now ;)