jQuery中attr和prop方法的区别

相比attr,prop是1.6.1才新出来的,二者从中文意思理解,都是获取/设置属性的方法(attributes和properties)。只是,window或document中使用.attr()方法在jQuery1.6以前不能正常运行,由于window和document中不能有attributes。prop应运而生了。html

既然咱们想知道他们两的区别,最好就看看他们的源代码,不要被代码长度所吓到,咱们只看关键的几句:node

attr方法代码(jQuery版本1.8.3)性能

    attr: function( elem, name, value, pass ) {
    var ret, hooks, notxml,
    nType = elem.nodeType;
    // don't get/set attributes on text, comment and attribute nodes
    if ( !elem || nType === 3 || nType === 8 || nType === 2 ) {
    return;
    }
    if ( pass && jQuery.isFunction( jQuery.fn[ name ] ) ) {
    return jQuery( elem )[ name ]( value );
    }
    // Fallback to prop when attributes are not supported
    if ( typeof elem.getAttribute === "undefined" ) {
    return jQuery.prop( elem, name, value );
    }
    notxml = nType !== 1 || !jQuery.isXMLDoc( elem );
    // All attributes are lowercase
    // Grab necessary hook if one is defined
    if ( notxml ) {
    name = name.toLowerCase();
    hooks = jQuery.attrHooks[ name ] || ( rboolean.test( name ) ? boolHook : nodeHook );
    }
    if ( value !== undefined ) {
    if ( value === null ) {
    jQuery.removeAttr( elem, name );
    return;
    } else if ( hooks && "set" in hooks && notxml && (ret = hooks.set( elem, value, name )) !== undefined ) {
    return ret;
    } else {
    elem.setAttribute( name, value + "" );
    return value;
    }
    } else if ( hooks && "get" in hooks && notxml && (ret = hooks.get( elem, name )) !== null ) {
    return ret;
    } else {
    ret = elem.getAttribute( name );
    // Non-existent attributes return null, we normalize to undefined
    return ret === null ?
    undefined :
    ret;
    }
    }


prop方法代码(jQuery版本1.8.3)code

    prop: function( elem, name, value ) {
    var ret, hooks, notxml,
    nType = elem.nodeType;
    // don't get/set properties on text, comment and attribute nodes
    if ( !elem || nType === 3 || nType === 8 || nType === 2 ) {
    return;
    }
    notxml = nType !== 1 || !jQuery.isXMLDoc( elem );
    if ( notxml ) {
    // Fix name and attach hooks
    name = jQuery.propFix[ name ] || name;
    hooks = jQuery.propHooks[ name ];
    }
    if ( value !== undefined ) {
    if ( hooks && "set" in hooks && (ret = hooks.set( elem, value, name )) !== undefined ) {
    return ret;
    } else {
    return ( elem[ name ] = value );
    }
    } else {
    if ( hooks && "get" in hooks && (ret = hooks.get( elem, name )) !== null ) {
    return ret;
    } else {
    return elem[ name ];
    }
    }
    }


attr方法里面,最关键的两行代码,elem.setAttribute( name, value + “” )和ret = elem.getAttribute( name ),很明显的看出来,使用的DOM的API setAttribute和getAttribute方法操做的属性元素节点。orm

而prop方法里面,最关键的两行代码,return ( elem[ name ] = value )和return elem[ name ],你能够理解成这样document.getElementById(el)[name] = value,这是转化成JS对象的一个属性。xml


既然明白了原理是这样,咱们来看看一个例子:htm

    <input type="checkbox" id="test" abc="111" />
    $(function(){
    el = $("#test");
    console.log(el.attr("style")); //undefined
    console.log(el.prop("style")); //CSSStyleDeclaration对象
    console.log(document.getElementById("test").style); //CSSStyleDeclaration对象
    });


    el.attr(“style”)输出undefined,由于attr是获取的这个对象属性节点的值,很显然此时没有这个属性节点,天然输出undefined对象

    el.prop(“style”)输出CSSStyleDeclaration对象,对于一个DOM对象,是具备原生的style对象属性的,因此输出了style对象rem

    至于document.getElementById(“test”).style和上面那条同样字符串


咱们接着看:

    el.attr("abc","111")
    console.log(el.attr("abc")); //111
    console.log(el.prop("abc")); //undefined


首先用attr方法给这个对象添加abc节点属性,值为111,能够看到html的结构也变了

    el.attr(“abc”)//输出结果为111,再正常不过了
    el.prop(“abc”)//输出undefined,由于abc是在这个的属性节点中,因此经过prop是取不到的


咱们再接着来:

    el.prop("abc", "222");
    console.log(el.attr("abc")); //111
    console.log(el.prop("abc")); //222


咱们再用prop方法给这个对象设置了abc属性,值为222,能够看到html的结构是没有变化的。输出的结果就不解释了。


上面已经把原理讲清楚了,何时用什么就能够本身把握了。


提一下,在遇到要获取或设置checked,selected,readonly和disabled等属性时,用prop方法显然更好,好比像下面这样:

    <input type="checkbox" id="test" checked="checked" />
    console.log(el.attr("checked")); //checked
    console.log(el.prop("checked")); //true
    console.log(el.attr("disabled")); //undefined
    console.log(el.prop("disabled")); //false


显然,布尔值比字符串值让接下来的处理更合理。


PS一下,若是你有JS性能洁癖的话,显然prop的性能更高,由于attr须要访问DOM属性节点,访问DOM是最耗时的。这种状况适用于多选项全选和反选的状况。 

相关文章
相关标签/搜索