關(guān)于它們兩個的區(qū)別,網(wǎng)上的答案很多。這里談?wù)劦暮芎唵蔚男牡茫?/p>
對于HTML元素本身就帶有的固有屬性,在處理時,使用prop方法。
對于HTML元素我們自己自定義的DOM屬性,在處理時,使用attr方法。
在jQuery中,attr()函數(shù)和prop()函數(shù)都用于設(shè)置或獲取指定的屬性,它們的參數(shù)和用法也幾乎完全相同。
但不得不說的是,這兩個函數(shù)的用處卻并不相同。下面我們來詳細(xì)介紹這兩個函數(shù)之間的區(qū)別。
1、操作對象不同
很明顯,attr和prop分別是單詞attribute和property的縮寫,并且它們均表示"屬性"的意思。
不過,在jQuery中,attribute和property卻是兩個不同的概念。attribute表示HTML文檔節(jié)點的屬性,property表示JS對象的屬性。
<!-- 這里的id、class、data_id均是該元素文檔節(jié)點的attribute --> <p id="message" class="test" data_id="123"></p> <script type="text/javascript"> // 這里的name、age、url均是obj的property var obj = { name: "CodePlayer", age: 18, url: "http://www.gxlcms.com/" }; </script>
在jQuery中,prop()函數(shù)的設(shè)計目標(biāo)是用于設(shè)置或獲取指定DOM元素(指的是JS對象,Element類型)上的屬性(property);attr()函數(shù)的設(shè)計目標(biāo)是用于設(shè)置或獲取指定DOM元素所對應(yīng)的文檔節(jié)點上的屬性(attribute)。
<!-- attr()函數(shù)針對的是該文檔節(jié)點的attribute --> <p id="message" class="test" data_id="123"></p> <script type="text/javascript"> // prop()函數(shù)針對的是該DOM元素(msg)自身的property var msg = document.getElementById("message"); var $msg = $(msg); </script>
當(dāng)然,在jQuery的底層實現(xiàn)中,函數(shù)attr()和prop()的功能都是通過JS原生的Element對象(如上述代碼中的msg)實現(xiàn)的。attr()函數(shù)主要依賴的是Element對象的getAttribute()和setAttribute()兩個方法。prop()函數(shù)主要依賴的則是JS中原生的對象屬性獲取和設(shè)置方式。
<p id="message" class="test" data_id="123"></p> <script type="text/javascript"> var msg = document.getElementById("message"); var $msg = $(msg); /* *** attr()依賴的是Element對象的element.getAttribute( attribute ) 和 element.setAttribute( attribute, value ) *** */ // 相當(dāng)于 msg.setAttribute("data_id", 145); $msg.attr("data_id", 145); // 相當(dāng)于 msg.getAttribute("data_id"); var dataId = $msg.attr("data_id"); // 145 /* *** prop()依賴的是JS原生的 element[property] 和 element[property] = value; *** */ // 相當(dāng)于 msg["pid"] = "pid值"; $msg.prop("pid", "pid值"); // 相當(dāng)于 msg["pid"]; var testProp = $msg.prop("pid"); // pid值 </script>
當(dāng)然,jQuery對這些操作方式進行了封裝,使我們操作起來更加方便(比如以對象形式同時設(shè)置多個屬性),并且實現(xiàn)了跨瀏覽器兼容。
此外,雖然prop()針對的是DOM元素的property,而不是元素節(jié)點的attribute。不過DOM元素某些屬性的更改也會影響到元素節(jié)點上對應(yīng)的屬性。例如,property的id對應(yīng)attribute的id,property的className對應(yīng)attribute的class。
<p id="message" class="test" data_id="123"></p> <script type="text/javascript"> var msg = document.getElementById("message"); var $msg = $(msg); document.writeln( $msg.attr("class") ); // test $msg.prop("className", "newTest"); // 修改className(property)導(dǎo)致class(attitude)也隨之更改 document.writeln( $msg.attr("class") ); // newTest </script>
2、應(yīng)用版本不同
attr()是jQuery 1.0版本就有的函數(shù),prop()是jQuery 1.6版本新增的函數(shù)。毫無疑問,在1.6之前,你只能使用attr()函數(shù);1.6及以后版本,你可以根據(jù)實際需要選擇對應(yīng)的函數(shù)。
3、用于設(shè)置的屬性值類型不同
由于attr()函數(shù)操作的是文檔節(jié)點的屬性,因此設(shè)置的屬性值只能是字符串類型,如果不是字符串類型,也會調(diào)用其toString()方法,將其轉(zhuǎn)為字符串類型。
prop()函數(shù)操作的是JS對象的屬性,因此設(shè)置的屬性值可以為包括數(shù)組和對象在內(nèi)的任意類型。
4、其他細(xì)節(jié)問題
在jQuery 1.6之前,只有attr()函數(shù)可用,該函數(shù)不僅承擔(dān)了attribute的設(shè)置和獲取工作,還同時承擔(dān)了property的設(shè)置和獲取工作。例如:在jQuery 1.6之前,attr()也可以設(shè)置或獲取tagName、className、nodeName、nodeType等DOM元素的property。
直到j(luò)Query 1.6新增prop()函數(shù),并用來承擔(dān)property的設(shè)置或獲取工作之后,attr()才只用來負(fù)責(zé)attribute的設(shè)置和獲取工作。
此外,對于表單元素的checked、selected、disabled等屬性,在jQuery 1.6之前,attr()獲取這些屬性的返回值為Boolean類型:如果被選中(或禁用)就返回true,否則返回false。
但是從1.6開始,使用attr()獲取這些屬性的返回值為String類型,如果被選中(或禁用)就返回checked、selected或disabled,否則(即元素節(jié)點沒有該屬性)返回undefined。并且,在某些版本中,這些屬性值表示文檔加載時的初始狀態(tài)值,即使之后更改了這些元素的選中(或禁用)狀態(tài),對應(yīng)的屬性值也不會發(fā)生改變。
因為jQuery認(rèn)為:attribute的checked、selected、disabled就是表示該屬性初始狀態(tài)的值,property的checked、selected、disabled才表示該屬性實時狀態(tài)的值(值為true或false)。
因此,在jQuery 1.6及以后版本中,請使用prop()函數(shù)來設(shè)置或獲取checked、selected、disabled等屬性。對于其它能夠用prop()實現(xiàn)的操作,也盡量使用prop()函數(shù)。
<input id="uid" type="checkbox" checked="checked" value="1"> <script type="text/javascript"> // 當(dāng)前jQuery版本為1.11.1 var uid = document.getElementById("uid"); var $uid = $(uid); document.writeln( $uid.attr("checked") ); // checked document.writeln( $uid.prop("checked") ); // true // 取消復(fù)選框uid的選中(將其設(shè)為false即可) // 相當(dāng)于 uid.checked = false; $uid.prop("checked", false); // attr()獲取的是初始狀態(tài)的值,即使取消了選中,也不會改變 document.writeln( $uid.attr("checked") ); // checked // prop()獲取的值已經(jīng)發(fā)生變化 document.writeln( $uid.prop("checked") ); // false </script>
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com