jQuery报错 uncaught exception type property can’t be changed

来自http://tunps.com/jquery-error-uncaught-exception-type-property-cant-be-changed

最近写了一个测试twiiter、嘀咕等api的测试程序,因为是测试,没有使用OAuth,直接采用HTTP Authentication 。

有两个文本框,分别用户输入用户名和密码,当鼠标焦点放在密码输入框上面的时候,改为将input的type属性由text改为password,就是输入的文字都变成"●●●●●●●"

语句就是

$("#pswd").focus(function(){
    $(this).attr('type','password');
});

结果效果没有实现,报错

搜索到此文, jQuery的某段源代码如下

// We can't allow the type property to be changed (since it causes problems in IE)
if ( name == "type" && jQuery.nodeName( elem, "input" ) && elem.parentNode )
    throw "type property can't be changed";

看样子是jQuery为了防止IE引发安全问题。所以只是jQuery不允许这样写,你如果你非要改变type,可以直接用javascript来完成:

var pass = document.createElement('input');
pass.type = 'password';
document.body.appendChild(pass);
pass.type = 'text';
pass.value = 'Password';

搞清楚了这样问题,那么我那个测试程序怎么做了,还是那篇文章给了灵感,可以放两个input,根据onfocus()和onblur()事件来show()或者hide()。

HTML code:

<input id="fakepswd" type="text" value="输入密码..." />
<input id="pswd" name="pswd" type="password" />

JS code:

//帐号
$("#user").focus(function(){
    if($("#user").val()=="输入帐号...")
    {
        $("#user").val("");
    }
});
$("#user").blur(function(){
    if($("#user").val()=="")
    {
        $("#user").val("输入帐号...");
    }
});
//密码
$("#fakepswd").focus(function(){
    $(this).attr('type','password');
});
$("#pswd").blur(function(){
    if($("#pswd").val()=="")
    {
        $("#fakepswd").show();
        $("#pswd").hide();
    }
});

About tunpishuang

just 4 fun·····
This entry was posted in 未分类 and tagged . Bookmark the permalink.

One Response to jQuery报错 uncaught exception type property can’t be changed

  1. 小范 says:

    非常感谢!!!解决了我困惑的问题!!

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>