標籤:word nodetype 它的 java 區別 適用於 其他 情況 logs
最近用jquery比較多,處理屬性的時候發現之前並沒有注意到的問題。
平時處理屬性都用attr,比如擷取checkbox的checked值 或者設定checkbox的checked的值
<input type="checkbox" id="allCheck"> $(‘#allCheck‘).attr(‘checked‘)$(‘#allCheck‘).attr(‘checked‘,‘checked‘) //發現選項並未被選中,只是在有些情況下發生
轉自:思考中
先提出問題:對於 checked 這類值是 true/false 的屬性,用 jQuery 的 attr 或 prop 方法進行 讀取或設定值是有區別的。
在看 jQuery 文檔前,我們先看看 attribute 與 property 是什麼:
先搞懂 attribute 與 property
當編寫 HTML 源碼時,你能在 HTML 元素裡定義 attributes。然後,一旦瀏覽器解析你的代碼,該 HTML 元素相應的 DOM 節點就會被建立。該節點是一個對象,因此它就擁有 properties。因此,我們知道:attributes 是 HTML 元素(標籤)的屬性,而 properties 是 DOM 對象的屬性。
例如,下面這個 HTML 元素:
<input type="text" value="Name:">
擁有兩個 attributes。
一旦瀏覽器解析該代碼,HTMLInputElement 對象就會被建立,並且該對象會擁有很多 peoperties,如:accept、accessKey、align、alt、attributes、autofocus、baseURI、checked、childElementCount、ChildNodes、children、classList、className、clientHeight 等等。
對於某個 DOM 節點對象,properties 是該對象的所有屬性,而 attributes 是該對象對應元素(標籤)的屬性。
當一個 DOM 節點為某個 HTML 元素所建立時,其大多數 properties 與對應 attributes 擁有相同或相近的名字,但這並不是一對一的關係。例如,下面這個 HTML 元素:
<input id="the-input" type="text" value="Name:">
其對應 DOM 節點會擁有如下properties: id、type 和 value:
-
id property是 id attribute 的映射:擷取該 property 即等於讀取其對應的 attribute 值,而設定該 property 即為 attribute 賦值。id 是一個純粹的映射 property,它不會修改或限制其值。
-
type property 是 type attribute 的映射:擷取該 property 即等於讀取其對應的 attribute 值,而設定該 property 即為 attribute 賦值。但 type 並不是一個純粹的映射 property,因為它的值被限制在已知值(即 input 的合法類型,如:text、password)。如果你有 <input type="foo">,然後 theInput.getAttribute("type") 會返回 "foo",而 theInput.type 會返回 "text"。
-
相比之下,value property 並不會映射 value attribute。取而代之的是 input 的當前值。當使用者手動更改輸入框的值,valueproperty 會反映該改變。所以,如果使用者在 input 輸入 John,然後:
theInput.value // 返回 "John"
然而:
theInput.getAttribute(‘value‘) // 返回 "Name:"
value property 反映了 input 的當前常值內容,而 value attribute 則是在 HTML 源碼 value 屬性所指定的初始常值內容。
因此,如果你想知道文字框的當前值,則讀取 property。而如果你想知道文字框的初始值,則讀取 attribute。或者你也可以利用 defaultValue property,它是 value attribute 的純粹映射。
theInput.value // returns "John"theInput.getAttribute(‘value‘) // returns "Name:"theInput.defaultValue // returns "Name:"
有幾個 properties 是直接反映它們 attribute(rel、id),而有一些則用稍微不同的名字進行直接映射(htmlFor 映射 forattribute,className 映射 class attribute)。很多 property 所映射的 attribute 是帶有限制/變動的(src、href、disabled、multiple)。該 規範 涵蓋了各種各樣的映射。
再看看 attr() 與 prop() 的區別
上述能讓我們理清了 attribute 與 property 之間的區別,下面根據 jQuery 文檔 對 attr() 與 prop() 方法進行比較:
自 jQuery 1.6 版本起,attr() 方法對於未設定的 attributes (即標籤中沒寫該 attributes)都會返回 undefined。對於檢索和改變 DOM 的 properties,如表單元素的 checked、selected 或 disabled 狀態,應使用 .prop() 方法。
Attributes vs. Properties
attributes 與 properties 之間的差異在特定情況下會變得尤為重要。在 jQuery 1.6 前,.attr() 方法在檢索一些 attributes 時,有時會把 property 考慮進去,這會導致不一致的行為。在 jQuery 1.6 版本之後,.prop() 方法提供了一種明確檢索 property 值的方式,而 .attr 只會檢索 attributes。
例如,selectedIndex、tagName、nodeName、nodeType、ownerDocument、defaultChecked 和 defaultSelected 能被 .prop() 檢索與設定。在 jQuery 1.6 之前,這些 properties 都是通過 .attr() 檢索的,但檢索這些屬性並不應屬於 attr 方法職責內 。這些屬性並沒有對應的 attributes,只有 properties 本身。
對於值為布爾值的 attributes ,考慮到一個 DOM 元素是通過 HTML 標籤 <input type="checkbox" checked="checked /> 定義的,並且假定它在 JavaScript 的變數名為 elem:
| 讀取屬性 |
傳回值 |
描述 |
| elem.checked |
true (Boolean) |
會隨著 checkbox 狀態作出相應改變 |
| $(elem).prop("checked") |
true (Boolean) |
會隨著 checkbox 狀態作出相應改變 |
| elem.getAttribute("checked") |
"checked" (String) |
checkbox 的初始狀態;並且不會隨著 checkbox 的狀態而改變。 |
| $(elem).attr("checked") (1.6) |
"checked" (String) |
checkbox 的初始狀態;並且不會隨著 checkbox 的狀態而改變。 |
| $(elem).attr("checked") (1.6.1+) |
"checked" (String) |
會隨著 checkbox 狀態而作出相應改變(與jQuery文檔描述不一樣,我用jQuery 1.12.1 測試,都是返回 “checked”,並不會隨著checkbox的改變而改變)。 |
| $(elem).attr("checked") (1.6之前版本) |
true (Boolean) |
true (Boolean) 會隨著 checkbox 狀態作出相應改變。 |
根據 W3C forms(表單) 規範,checked 是一個值為 boolean 的 attribute,這意味著當該 attribute 存在(無論值是什麼),其對應的 property 都是 true。例如,該 attribute 沒賦值或設為空白字串,甚至設為 "false"。這同樣適用於所有值為 boolean 的 attributes。
然而,對於 checked attribute 最重要的概念是記住它並不是對應 checked property。該 attribute 實際上是對應 defaultCheckedproperty,並僅在初次設定 checkbox 值時使用。checked attribute 的值並不會隨著 checkbox 的狀態而作出相應改變,而 checkedproperty 會。因此,為了相容不同瀏覽器,當判斷一個 checkbox 是否被選擇時應該使用 property:
if (elem.checked)if ($(elem).prop("checked"))if ($(elem).is(":checked"))
這同樣適用於其它動態 attributes,如 selected 和 value。
其他說明:
在 IE9 之前的版本,如果使用 .prop() 為 DOM 元素的 property 設定的值不是一個簡單的原始值(number、string 或 boolean),且該 property 在 DOM 元素從 document 移除前未被移除(使用 .removeProp()),則會導致記憶體流失。為 DOM 對象設定值的安全做法(避免記憶體流失)是使用 .data()。
簡單來說:為了避免一些特殊情況引起的差異,可以這樣,DOM元素原生屬性盡量用prop,自訂屬性盡量用attr
歡迎提不同意見~
jquery之prop和attr區別