標籤:blog http io ar 使用 java for strong sp
DOM的attribute和property
今天講一個JS的小話題,就是DOM節點的attribute和property的區別,這個點看起來很小,其實背後別有洞天。如果面試一個前端,聽他講講對這個問題的理解,基本能分辨是不是菜鳥。下面的內容請各位細心體會。
頁面有一個INPUT輸入框
這個比較簡單,沒什麼好說的,兩個值都是’1′。現在我把輸入框的值改成100,然後重新取一遍值:
為什麼兩個值不同了?這就是attribute和property在作怪。
Attibute
這 個INPUT節點有很多屬性(attribute):’type’,’id’,’value’,’class’以及自訂的’data- explain’。javascript的DOM模型提供了兩個方法getAttribute()和setAttribute() 讀寫DOM樹節點屬性(attribute)值。注意這裡的getAttribute()方法有個潛規則:部分特殊屬性(比如INPUT的value和 checked),getAttribute取到的是初始值,這就解釋了修改輸入框值為100後,v1取到的還是初始值’1′。
Property
Javascript 擷取到的DOM節點對象,比如a,你可以把它看作一個基本JS對象(儘管某些瀏覽器實現上並不完全是),這個對象實現了很多屬性 (property):’value’,’className’等,以及一些方法還有前面說的 getAttribute,setAttribute,onclick等。注意這個對象的’value’屬性(property)取的值是輸入框內的當前 值。所以v2的值是’100′。
推薦大家盡量使用property,這樣事情處理起來簡單一下,attribute的值永遠是字串,比如:
另 外,由於瀏覽器實現細節的差異,也存在一些坑,比如:在Chrome,Firefox及IE8之後,標籤的attribute名是大小寫不敏感 的,DOM.setAttribute(‘myname’,’xxx’)可以通過DOM.getAttribute(‘MyName’)來取,另外在 XHTML文檔模式下,設定’tabIndex’為任意值會將’tabindex’值置為0。
對於attribute和property,jquery中的實現其實挺糾結的,大家應該知道jQ的跟他們相關的三個方法 .attr()、.prop()以及.data(),這三個背後也有淵源。
jQuery .attr() .prop() .data()
首先看看三個方法的解釋:
.attr(),此方法從jq1.0開始一直存在,官方文檔寫的作用是讀/寫DOM的attribute值,其實1.6之前有時候是attribute,有時候又是property。.prop(),此方法jq1.6引入,讀/寫DOM的property。.data(),此方法在jq1.2.3引入,作用是把任意的值讀取/儲存到DOM元素對應的jq對象上。
先說.attr()和.prop(),在jq1.6版之前,沒有.prop()方法,而.attr()混淆了attribute和property的概念,導致使用時不小心就會出現bug,如果不讀jq源碼,還真說不清楚.attr()設定的是attribute還是property。
至於jq為什麼這樣設計,眾說紛紜。有人說jqTeam Dev自己也沒搞清楚attribute和property的區別,也有人說這是過度設計,jq認為使用者沒必要瞭解attribute和property的區別,乾脆封裝到一起了。不管原因是什麼,jq這個設計確實不好。
2011年5月份,jq在新版本1.6版中引入新的API .prop()方法,不過升級過程挺痛苦,1.6版對比1.5.2版在.attr()的引入上沒有做好向下相容,一些人升級jq到1.6之後,發現自己的代碼掛了。緊接著jq發布1.6.1版解決了相容問題。
對於加入 .prop() 方法,jq的解釋是:首先,給一些DOM對象屬性property(比如nodeName,selectedIndex)的擷取提供了更簡潔的方案,情況前後的對比:
// 不用 .prop()var elem = $("#foo")[0];if ( elem ) { index = elem.selectedIndex;}// 用 .prop()index = $("#foo").prop("selectedIndex");
另外還有一個原因是.prop()的效率好於.attr()。
下 面說說.data(),我們知道html 5裡面DOM標籤可以加以一些data-xxx的屬性,你可以把.data()看作是存取data-xxx這樣DOM附加資訊的方法。當 然,.data()存取的內容不僅是字串,還可以包含數組和對象。從jq1.4.3起,html5 的data-xxx屬性會自動被添加到jq的data對象裡,比如有下列代碼:
<div data-role="page" data-last-value="43" data-hidden="true" data-options=‘{"name":"noahlu"}‘></div>
下面的等式都成立:
$("div").data("role") === "page";$("div").data("lastValue") === 43;$("div").data("hidden") === true;$("div").data("options").name === "noahlu";
儘管.data()用來存資料挺方便,它也是有過不堪回首的過去,請看:
<button id="foo" data-key="1.4000">Click me</button><script>typeof $(‘#foo‘).data(‘key‘);</script>
jQuery 1.8之前的版本輸出’number’,1.8版本之後輸出為’string’。1.8版之前,data會把值轉換成基本類型,其實我們這裡想要的是’1.4000′並不是1.4。
從.data()的實際用途來看,跟.prop()似乎很像,都可以給DOM對象附加上一些自訂的值。我只能說,他們定位不同,看看他們的各自的方法名,再想想什麼情境用什麼方法吧。
最後,從效能上對比,.prop() > .data() > .attr(),不同瀏覽器不同版本.data()和.attr()的效能關係有差異,不過.prop()總是最優的。
參考資料
- .prop() vs .attr()
- jQuery 1.6.1 released
- jQuery 1.6 and .attr()
- jQuery doc .data()
- jQuery attr() prop() data() performance
- Don’t use the jQuery .data() method. Use .attr() instead.
上面的內容轉自:http://www.noahlu.com/blog/javascript-note/jquery-attr-prop-data/
上面的區別導致經常在做全選與全不選時等問題,所以在設定selectedIndex, tagName, nodeName, nodeType, ownerDocument, Checked, 或者defaultSelected時應該使用prop方法,而不是用attr方法。
查詢官方API得到解釋如下:
The .prop() method is a convenient way to set the value of properties—especially when setting multiple properties, using values returned by a function, or setting values on multiple elements at once. It should be used when setting selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, or defaultSelected. Since jQuery 1.6, these properties can no longer be set with the .attr() method. They do not have corresponding attributes and are only properties.
.prop()方法是一種簡便的設定值得方式,特別是多屬性、使用函數傳回值或者一次性在多個elements中設定數值。在設定selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, 或者defaultSelected時應該使用本方法。從jQuery1.6起,這些屬性不再能夠用.attr()方法設定了,它們沒有相當的attributes,只有properties。
Properties generally affect the dynamic state of a DOM element without changing the serialized HTML attribute. Examples include the value property of input elements, the disabledproperty of inputs and buttons, or the checked property of a checkbox. The .prop() method should be used to set disabled and checked instead of the .attr() method. The .val()method should be used for getting and setting value.
Properties可以改變DOM元素的動態月台,不適用序列化的attribue。比如input的value屬性、input和button 的disabled屬性,或者checkbox的checked屬性。這時應該使用.prop()來替代.attr()來設定disabled和 checked。.val()用於擷取或者設定其value值。
jQuery .attr() .prop() .data()區別及全選等問題