JS 动态修改 input 的 type 属性
2012-09-26 10:05
656 查看
很久没来部落格了,杂草丛生广告横行啊!工作比以前轻松很多人却越来越懒了…… 最近做一个登陆框,当鼠标焦点放在两个输入框上面的时候将输入框置空,简单说就是清除输入框的默认值,如图
这里写了一个jQuery对象方法的插件
(function($) {
jQuery.fn.extend({
“clearDefault”:function(className){
$(this).focus(function(){
if (className) {
$(this).addClass(className);
}
if($(this).val() ==this.defaultValue){
$(this).val(“”);
}
}).blur(function(){
if (className) {
$(this).removeClass(className);
}
if ($(this).val() == ”) {
$(this).val(this.defaultValue);
}
});
}
});
})(jQuery);
调用: $(“#uname”); 可选参数class为获取焦点时input的class,但是清除密码的时候出了一个问题,用户输入的密码应该是 ”*****” ,这里需要将input 的type 属性由 text 换成 password ,如果用户没有输入密码,鼠标失去焦点的时候 type 换回 text ,value 值为 “密码”。
$(“#pswd”).focus(function(){
$(this).attr(‘type’,'password’);
发现并没有实现预期效果,出现 uncaught exception type property can’t be changed 错误,查看jQuery 1.42源码 1488 行
// We can’t allow the type property to be changed (since it causes problems in IE)
if ( name === “type” && rtype.test( elem.nodeName ) && elem.parentNode ) {
jQuery.error( “type property can’t be changed” );
jQuery 修改不了用源生的JS呢?
$(“#pwd”).focus(function(){
$(“#pwd”)[0].type = ’password’;
$(“#pwd”).val(“”);
});
发现在FF下可以修改并将密码输入框type 修改为 “password” 并将 value设置为空,而IE下却提示无法得到type属性,不支持该命令。 弹出 type 看看真的无法得到吗?
$(“#pwd”).focus(function(){
alert($(“#pwd”)[0].type);
$(“#pwd”)[0].type = ’password’;
$(“#pwd”).val(“”);
});
发现弹出text ,原来不是无法得到,只是IE下不能修改。 各种 google 后发现input的type属性只能在初始的时候设定却不能修改(IE不能,FF可以,个人认为,不知道是否准确,如有不对请大牛指教)。 这样我是否可以先remove然后再生成一个type是password的密码输入框呢?
$(“#pwd”).focus(function(){
$(“#pwd”).remove();
$(“body”).append(‘<input id=“pwd” name=“pwd” type=“password” />’);
$(“#pwd”).focus(); // 焦点转移
});
可以实现,但是输入为空鼠标失去焦点再返回到起始状态的时候不怎么好写,节点生成删除为什么不用两个密码框来显示隐藏呢?
<input id=“showPwd” class=“txt” type=“text” value=“密码” tabindex=“2″ />
<input id=“pwd” class=“txt” name=“password” type=“password” />
var showPwd = $(“#showPwd”), pwd = $(“#pwd”);
showPwd.focus(function(){
pwd.show().focus();
showPwd.hide();
});
pwd.blur(function(){
if(pwd.val()==“”) {
showPwd.show();
pwd.hide();
}
});
完美解决。
这里写了一个jQuery对象方法的插件
(function($) {
jQuery.fn.extend({
“clearDefault”:function(className){
$(this).focus(function(){
if (className) {
$(this).addClass(className);
}
if($(this).val() ==this.defaultValue){
$(this).val(“”);
}
}).blur(function(){
if (className) {
$(this).removeClass(className);
}
if ($(this).val() == ”) {
$(this).val(this.defaultValue);
}
});
}
});
})(jQuery);
调用: $(“#uname”); 可选参数class为获取焦点时input的class,但是清除密码的时候出了一个问题,用户输入的密码应该是 ”*****” ,这里需要将input 的type 属性由 text 换成 password ,如果用户没有输入密码,鼠标失去焦点的时候 type 换回 text ,value 值为 “密码”。
$(“#pswd”).focus(function(){
$(this).attr(‘type’,'password’);
发现并没有实现预期效果,出现 uncaught exception type property can’t be changed 错误,查看jQuery 1.42源码 1488 行
// We can’t allow the type property to be changed (since it causes problems in IE)
if ( name === “type” && rtype.test( elem.nodeName ) && elem.parentNode ) {
jQuery.error( “type property can’t be changed” );
jQuery 修改不了用源生的JS呢?
$(“#pwd”).focus(function(){
$(“#pwd”)[0].type = ’password’;
$(“#pwd”).val(“”);
});
发现在FF下可以修改并将密码输入框type 修改为 “password” 并将 value设置为空,而IE下却提示无法得到type属性,不支持该命令。 弹出 type 看看真的无法得到吗?
$(“#pwd”).focus(function(){
alert($(“#pwd”)[0].type);
$(“#pwd”)[0].type = ’password’;
$(“#pwd”).val(“”);
});
发现弹出text ,原来不是无法得到,只是IE下不能修改。 各种 google 后发现input的type属性只能在初始的时候设定却不能修改(IE不能,FF可以,个人认为,不知道是否准确,如有不对请大牛指教)。 这样我是否可以先remove然后再生成一个type是password的密码输入框呢?
$(“#pwd”).focus(function(){
$(“#pwd”).remove();
$(“body”).append(‘<input id=“pwd” name=“pwd” type=“password” />’);
$(“#pwd”).focus(); // 焦点转移
});
可以实现,但是输入为空鼠标失去焦点再返回到起始状态的时候不怎么好写,节点生成删除为什么不用两个密码框来显示隐藏呢?
<input id=“showPwd” class=“txt” type=“text” value=“密码” tabindex=“2″ />
<input id=“pwd” class=“txt” name=“password” type=“password” />
var showPwd = $(“#showPwd”), pwd = $(“#pwd”);
showPwd.focus(function(){
pwd.show().focus();
showPwd.hide();
});
pwd.blur(function(){
if(pwd.val()==“”) {
showPwd.show();
pwd.hide();
}
});
完美解决。
相关文章推荐
- JS 动态修改 input 的 type 属性
- js动态修改input输入框的type属性(实现方法解析)
- JS 动态修改 input 的 type 属性 - teresa502的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/teresa502/article/details/6546361
- js动态修改input输入框的type属性(实现方法解析)
- js修改input的type属性问题
- js修改input的type属性及浏览器兼容问题探讨与解决
- js修改input的type属性问题探讨
- js修改input的type属性及浏览器兼容问题探讨与解决
- js修改input的type属性问题探讨
- js修改input的type属性问题探讨
- js修改input的type属性问题
- js修改input的type属性问题
- js修改input的type属性问题
- 用JS替 <input type=file>的value属性动态赋值
- js修改input的type属性问题
- js修改input的type属性问题(兼容所有浏览器,主要用于密码类的默认有提示文字的效果)
- [转]js修改input的type属性的问题
- 动态修改html input标签的属性
- 使用js控制input type=”check”属性
- 修改input的type属性