change type of input field with jQuery

后端 未结 29 2586
青春惊慌失措
青春惊慌失措 2020-11-22 05:13
$(document).ready(function() {
    // #login-box password field
    $(\'#password\').attr(\'type\', \'text\');
    $(\'#passwo         


        
29条回答
  •  没有蜡笔的小新
    2020-11-22 05:31

    It's very likely this action is prevented as part of the browser's security model.

    Edit: indeed, testing right now in Safari, I get the error type property cannot be changed.

    Edit 2: that seems to be an error straight out of jQuery. Using the following straight DOM code works just fine:

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

    Edit 3: Straight from the jQuery source, this seems to be related to IE (and could either be a bug or part of their security model, but jQuery isn't specific):

    // 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";
    

提交回复
热议问题