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對應的值,我們打印一下:
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高級程序設計》中提到,為了方便操作,建議大家用setAttribute()和getAttribute()來操作即可。
打印property
html自帶的dom屬性會自動轉換成property,但是自定義的屬性沒有這個'權利'
直接把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'));
}
猜一下結果??
答案是:
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
總結如下:
- 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
小結
分析了這么多,對property和attribute的區(qū)別理解也更深了,在這里總結一下:
創(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;
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
用函數(shù)式編程技術編寫優(yōu)美的 JavaScript
用函數(shù)式編程技術編寫優(yōu)美的 JavaScript...2006-11-11
javascript實現(xiàn)輸出指定行數(shù)正方形圖案的方法
這篇文章主要介紹了javascript實現(xiàn)輸出指定行數(shù)正方形圖案的方法,可實現(xiàn)javascript獲取用戶輸入及根據(jù)輸入?yún)?shù)打印圖形的功能,需要的朋友可以參考下2015-08-08
代碼實例ajax實現(xiàn)點擊加載更多數(shù)據(jù)圖片
在本篇文章里我們給大家分享了關于ajax實現(xiàn)點擊加載更多數(shù)據(jù)圖片的相關代碼知識點,有興趣的朋友們參考下。2018-10-10
javascript showModalDialog 多層模態(tài)窗口實現(xiàn)頁面提交及刷新的代碼
javascript 多層模態(tài)窗口showModalDialog頁面提交及刷新2009-11-11
小程序scroll-view安卓機隱藏橫向滾動條的實現(xiàn)詳解
這篇文章主要介紹了小程序scroll-view安卓機隱藏橫向滾動條的實現(xiàn)詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05
js有趣的非空判斷函數(shù) + ?? 實現(xiàn)
本文介紹了JS中空值合并操作符??的用法,包括如何使用它來判斷數(shù)據(jù)是否為“合法數(shù)據(jù)”,即非空字符串、非undefined和非null,下面就一起來了解一下,感興趣的可以了解一下2024-09-09
你必須知道的Javascript知識點之"深入理解作用域鏈"的介紹
本篇文章小編為大家介紹,你必須知道的Javascript知識點之"深入理解作用域鏈"的介紹。需要的朋友參考下2013-04-04

