相比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是最耗時的。這種狀況適用於多選項全選和反選的狀況。