JS中DOM元素的attribute與property屬性示例詳解
一、'表親戚':attribute和property
為什么稱attribute和property為'表親戚'呢?因為他們既有共同處,也有不同點.
attribute 是 dom 元素在文檔中作為 html 標簽擁有的屬性;
property 是 dom 元素在 js 中作為對象擁有的屬性。
從定義上可以看出:
- 對于 html 的標準屬性來說,attribute 和 property 是同步的,是會自動更新的
- 但是對于自定義的屬性來說,他們是不同步的.(自定義屬性不會自動添加到property)
- property 的值可以改變;attribute 的值不能改變
二、 兩者輸出形式
1、分別打印兩個值
打印attribute屬性
//html <div class="divClass" id="divId" ></div> //js window.onload = function(){ var divId = document.getElementById('divId'); console.log(divId.attributes); }
可以看見attributes對應(yīng)的值,我們打印一下:
console.log(divId.attributes[0]); //打印 class="divClass" console.log(divId.attributes.class) //打印 class="divClass" console.log(divId.getAttribute('class')) //打印divClass console.log(divId.getAttribute('id')) //打印divId
發(fā)現(xiàn)上面兩組值是相等的.
雖然都可以取值,但《js高級程序設(shè)計》中提到,為了方便操作,建議大家用setAttribute()和getAttribute()來操作即可。
打印property
html自帶的dom屬性會自動轉(zhuǎn)換成property,但是自定義的屬性沒有這個'權(quán)利'
直接把div標簽當作對象,用'.'輸出即是property屬性
但是注意!property是不能輸出自定義屬性的
<div class="divClass" id="divId" addUserDefine="zidingyi"></div> console.log(divId.class); //打印 divClass console.log(divId.addUserDefine) //打印 undefined
打開Elements的properties可以看到,dom存在的屬性,property同樣繼承了,而addUserDefine卻沒有出現(xiàn)在property中
property:
var obj = {}; Object.defineProperty(obj,'name',{ value:'Property' }) console.log(obj.name) //打印 Property
三、用例子解析兩者賦值
如果我們修改了property的值
//html <input value="initValue" id="ipt"/> //js window.onload = function(){ var ipt = document.getElementById('ipt'); ipt.value = 'changeValue' console.log(ipt.value); console.log(ipt.getAttribute('value')); }
猜一下結(jié)果??
答案是:
console.log(ipt.value); //changeValue console.log(ipt.getAttribute('value')); //initValue
我們再來看看input的值
難以置信?
我們再來看看從修改attribute入手
//html <input value="initValue" id="ipt"/> //js window.onload = function(){ var ipt = document.getElementById('ipt'); ipt.setAttribute('value','changeValue') console.log(ipt.value); console.log(ipt.getAttribute('value')); }
輸出:
console.log(ipt.value); //changeValue console.log(ipt.getAttribute('value')); //changeValue
總結(jié)如下:
- property比attribute'霸道',估計是'表哥'
- property和attribute兩者是屬于單方面通信,即:
1.property能夠從attribute中得到同步;
2.attribute不會同步property上的值;
再啰嗦一句:
對屬性Property可以賦任何類型的值,而對特性Attribute只能賦值字符串!
//js var obj = { value : false, } var ipt = document.getElementById('ipt'); obj.value = true; //property更改 ipt.setAttribute('value',true) //attribute更改 console.log(typeof obj.value); //boolean console.log(obj.value) //true console.log(typeof ipt.value) //string console.log(ipt.value); //true
小結(jié)
分析了這么多,對property和attribute的區(qū)別理解也更深了,在這里總結(jié)一下:
創(chuàng)建
- DOM對象初始化時會在創(chuàng)建默認的基本property;
- 只有在HTML標簽中定義的attribute才會被保存在property的attributes屬性中;
- attribute會初始化property中的同名屬性,但自定義的attribute不會出現(xiàn)在property中;
- attribute的值都是字符串;
數(shù)據(jù)綁定
- attributes的數(shù)據(jù)會同步到property上,然而property的更改不會改變attribute;
- 對于value,class這樣的屬性/特性,數(shù)據(jù)綁定的方向是單向的,attribute->property;
- 對于id而言,數(shù)據(jù)綁定是雙向的,attribute<=>property;
- 對于disabled而言,property上的disabled為false時,attribute上的disabled必定會并存在,此時數(shù)據(jù)綁定可以認為是雙向的;
使用
- 可以使用DOM的setAttribute方法來同時更改attribute;
- 直接訪問attributes上的值會得到一個Attr對象,而通過getAttribute方法訪問則會直接得到attribute的值;
- 大多數(shù)情況(除非有瀏覽器兼容性問題),jQuery.attr是通過setAttribute實現(xiàn),而jQuery.prop則會直接訪問DOM對象的property;
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
JavaScript使用小插件實現(xiàn)倒計時的方法講解
今天小編就為大家分享一篇關(guān)于JavaScript使用小插件實現(xiàn)倒計時的方法講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03JavaScript 對象深入學(xué)習(xí)總結(jié)(經(jīng)典)
JavaScript中,除了五種原始類型(即數(shù)字,字符串,布爾值,null,undefined)之外的都是對象了,所以,不把對象學(xué)明白怎么繼續(xù)往下學(xué)習(xí)呢?本篇文章給大家分享javascript對象深入學(xué)習(xí)總結(jié),小伙伴們跟著小編一起深入學(xué)習(xí)吧2015-09-09JavaScript方法_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了JavaScript方法,詳細的介紹了JavaScript幾種函數(shù)定義方式及使用方法,感興趣的小伙伴們可以參考一下2017-06-06