2018.3.1更:javascript
有贊·微商城部門招前端啦,最近的前端hc有十多個,跪求大佬扔簡歷,我直接進行內推實時反饋進度,有興趣的郵件 lvdada#youzan.com,或直接微信勾搭我 wsldd225 瞭解跟多html
有贊開源組件庫·zanUI前端
原文:http://joji.me/en-us/blog/html-attribute-vs-dom-propertyjava
當咱們經過js處理DOM對象時很是容易將attribute(特性)和property(屬性)混淆。document.getElementById('test').getAttribute('id')
,$('#test').attr('id'), document.getElementById('test').id
還有$('#test').prop('id')
三者都返回相同的id:「test」。這篇文章我將解釋attribute和property的區別。jquery
<div id="test" class="button" custom-attr="1"></div>
複製代碼
document.getElementById('test').attributes;
// return: [custom-attr="hello", class="button", id="test"]
複製代碼
document.getElementById('test').getAttribute('custom-attr')
或者$('#test').attr('custom-attr')
老是返回字符串類型的"1"。document.getElementById('test').foo = 1; // 設置屬性: foo 爲 number類型: 1
document.getElementById('test').foo; // 獲取屬性值, return number: 1
$('#test').prop('foo'); // 使用jquery獲取屬性值, return number: 1
複製代碼
$('#test').prop('foo', {
age: 23,
name: 'John'
}); // 使用jquery設置一個名爲foo的對象
document.getElementById('test').foo.age; // return number類型: 23
document.getElementById('test').foo.name; // return string類型: "John"
複製代碼
**譯者注:**這裏的property能夠是任意類型指的是咱們爲DOM對象自定義添加的屬性,對於DOM對象的原始屬性,相似name
屬性,不管咱們設置什麼類型的值,最後返回的都是字符類型。瀏覽器
另外,咱們獲取HTML5定義的data屬性時,獲取的值也是字符串。<div data-id="33"></div>
,ele.dataset.id // string 33
bash
<div id="test" class="button" foo="1"></div>
複製代碼
document.getElementById('test').id; // return string: "test"
document.getElementById('test').className; // return string: "button"
document.getElementById('test').foo; // return undefined 由於foo是一個自定義的attr特性
複製代碼
注意:當咱們經過property屬性進行設置或獲取class時,咱們須要使用**"className"**,由於在js中class
是關鍵字。微信
**譯者注:**第二點的意思是說當咱們在html中寫非自定義的attribute特性時,DOM對象會自動映射對應的propertydom
<div id="test" class="button"></div>
複製代碼
var div = document.getElementById('test');
div.className = 'red-input';
div.getAttribute('class'); // return string: "red-input"
div.setAttribute('class','green-input');
div.className; // return string: "green-input"
複製代碼
<input id="search" value="foo" />
複製代碼
var input = document.getElementById('search');
input.value = 'foo2';
input.getAttribute('value'); // return string: "foo"
複製代碼
**譯者注:**這條特性意味着咱們平時在寫業務的時候多數狀況下使用property是正確的。當用戶input輸入更改的時候,attribute-value值不會變化,即便js更改value,也不會使attribute變化。這也驗證了第三點的。ui
在javascript中咱們推薦使用property屬性由於這個屬性相對attribute更快,更簡便。尤爲是有些類型本該是布爾類型的attribute特性。好比:"checked", "disabled", "selected"。瀏覽器會自動將這些值轉變成布爾值傳給property屬性。
<input id="test" class="blue" type="radio" />
複製代碼
好實踐
// get id
document.getElementById('test').id;
// set class
document.getElementById('test').className = 'red';
// get and set radio control status
document.getElementById('test').checked; // boolean
document.getElementById('test').checked = true;
$('#test').prop('checked'); // boolean
$('#test').prop('checked', true);
複製代碼
壞實踐
// get id
document.getElementById('test').getAttribute('id');
// set class
document.getElementById('test').setAttribute('class', 'red');
document.getElementById('test').getAttribute('checked'); // 返回字符串類型 'checked'
複製代碼
本文來自二口南洋,有什麼須要討論的歡迎找我。