JavaScript中的property和attribute介紹
property ['prɔpəti]
n. 性質(zhì),性能;財(cái)產(chǎn);所有權(quán)
英英釋義:
any area set aside for a particular purpose “the president was concerned about the property across from the White House”
同義詞:place
something owned; any tangible or intangible possession that is owned by someone “that hat is my property”; ” he is a man of property”
同義詞:belongings | holding | material possession
a basic or essential attribute shared by all members of a class
a construct whereby objects or individuals can be distinguished “self-confidence is not an endearing property”
同義詞:attribute | dimension
any movable articles or objects used on the set of a play or movie
同義詞:prop
重點(diǎn)看2、3、4條。
再看attribute:
attribute [ə'tribju:t, 'ætribju:t]
n. 屬性;特質(zhì)
vt. 歸屬;把…歸于
英英釋義:
n.
a construct whereby objects or individuals can be distinguished
同義詞:property | dimension
an abstraction belonging to or characteristic of an entity
v.
attribute or credit to ”We attributed this quotation to Shakespeare”
同義詞:impute | ascribe | assign
decide as to where something belongs in a scheme
同義詞:assign
property,attribute都作“屬性”解,但是attribute更強(qiáng)調(diào)區(qū)別于其他事物的特質(zhì)/特性,而在這篇文章中也提交到attribute是property的子集。
而在JavaScript中,property和attribute更是有明顯的區(qū)別。眾所周知,setAttribute是為DOM節(jié)點(diǎn)設(shè)置/添加屬性的標(biāo)準(zhǔn)方法:
var ele = document.getElementById("my_ele"); ele.setAttribute("title","it's my element");但很多時(shí)候我們也這樣寫(xiě):
ele.title = "it's my element";如果不出什么意外,他們都運(yùn)行的很好,它們似乎毫無(wú)區(qū)別?而且通常情況下我們還想獲取到我們?cè)O(shè)置的“屬性”,我們也很愛(ài)這樣寫(xiě):
alert(ele.title);這時(shí)候,你便會(huì)遇到問(wèn)題,如果你所設(shè)置的屬性屬于DOM元素本身所具有的標(biāo)準(zhǔn)屬性,不管是通過(guò)ele.setAttribute還是ele.title的方式設(shè)置,都能正常獲取。但是如果設(shè)置的屬性不是標(biāo)準(zhǔn)屬性,而是自定義屬性呢?
ele.setAttribute('mytitle','test my title'); alert(ele.mytitle); //undefined alert(ele.getAttribute('mytitle')); //'test my title' ele.yourtitle = 'your test title'; alert(ele.getAttribute('yourtitle')); //null alert(ele.yourtitle); //'your test title'通過(guò)setAttribute設(shè)置的自定義屬性,只能通過(guò)標(biāo)準(zhǔn)的getAttribute方法來(lái)獲??;同樣通過(guò)點(diǎn)號(hào)方式設(shè)置的自定義屬性也無(wú)法通過(guò) 標(biāo)準(zhǔn)方法getAttribute來(lái)獲取。在對(duì)自定義屬性的處理方式上,DOM屬性的標(biāo)準(zhǔn)方法和點(diǎn)號(hào)方法不再具有任何關(guān)聯(lián)性(上訴代碼在IE6-有兼容性 問(wèn)題,后面會(huì)繼續(xù)介紹)。
這種設(shè)置、獲取“屬性”的差異性,究其根源,其實(shí)也是property與attribute的差異性所致。
通過(guò)點(diǎn)號(hào)設(shè)置的“屬性”其實(shí)是設(shè)置的property,如上所說(shuō)attribute是property的子集,那么點(diǎn)號(hào)設(shè)置的property自然無(wú)法通過(guò)只能獲取attribute的getAttribute方法來(lái)獲取。
property and attribute
照?qǐng)D似乎更易理解,getAttribute無(wú)法獲取到不屬于attribute的property也是理所應(yīng)當(dāng)。但是這時(shí)候你會(huì)發(fā)現(xiàn)另外一個(gè)問(wèn)題,通過(guò)setAttribute設(shè)置的屬性,同樣也應(yīng)該屬于property,那么為何無(wú)法通過(guò)點(diǎn)號(hào)獲?。?/P>
我們換種理解,只有標(biāo)準(zhǔn)屬性才可同時(shí)使用標(biāo)準(zhǔn)方法和點(diǎn)號(hào)方法,而對(duì)于自定義屬性,標(biāo)準(zhǔn)方法和點(diǎn)號(hào)方法互不干擾。
自定義屬性互不干擾
但在IE9-中,上訴結(jié)論并不成立。IE9-瀏覽器中,除了標(biāo)準(zhǔn)屬性,自定義屬性也是共享的,即標(biāo)準(zhǔn)方法和點(diǎn)號(hào)皆可讀寫(xiě)。
成功設(shè)置的attribute都會(huì)體現(xiàn)在HTML上,通過(guò)outerHTML可以看到attribute都被添加到了相應(yīng)的tag上,所以如果 attribute不是字符串類(lèi)型數(shù)據(jù)都會(huì)調(diào)用toString()方法進(jìn)行轉(zhuǎn)換。但是由于IE9-中,標(biāo)準(zhǔn)屬性與自定義屬性不做區(qū) 分,attribute依然可以是任意類(lèi)型數(shù)據(jù),并不會(huì)調(diào)用toString()轉(zhuǎn)換,非字符串a(chǎn)ttribute不會(huì)體現(xiàn)在HTML上,但更為嚴(yán)重的問(wèn) 題是,這樣很容易就會(huì)導(dǎo)致內(nèi)存泄漏。所以如果不是字符串類(lèi)型的自定義屬性,建議使用成熟框架中的相關(guān)方法(如jQuery中的data方法)。
getAttribute與點(diǎn)號(hào)(.)的差異性
雖然getAttribute和點(diǎn)號(hào)方法都能獲取標(biāo)準(zhǔn)屬性,但是他們對(duì)于某些屬性,獲取到的值存在差異性,比如href,src,value等。
<a href="#" id="link">Test Link</a> <img src="img.png" id="image" /> <input type="text" value="enter text" id="ipt" /> <script> var $ = function(id){return document.getElementById(id);}; alert($('link').getAttribute('href'));//# alert($('link').href);//fullpath/file.html# alert($('image').getAttribute('src'))//img.png alert($('image').src)//fullpath/img.png alert($('ipt').getAttribute('value'))//enter text alert($('ipt').value)//enter text $('ipt').value = 5; alert($('ipt').getAttribute('value'))//enter text alert($('ipt').value)//5 </script>測(cè)試可發(fā)現(xiàn)getAttribute獲取的是元素屬性的字面量,而點(diǎn)號(hào)獲取的是計(jì)算值。
更多細(xì)節(jié)可查看這篇文章:Attributes and custom properties
相關(guān)文章
JavaScript常見(jiàn)JSON操作實(shí)例分析
這篇文章主要介紹了JavaScript常見(jiàn)JSON操作,簡(jiǎn)單描述了json的概念、常見(jiàn)json類(lèi)型,并結(jié)合實(shí)例形式分析了json的序列化、轉(zhuǎn)換、格式化、解析等相關(guān)操作技巧,需要的朋友可以參考下2018-08-08
Three.js實(shí)現(xiàn)繪制字體模型示例代碼
最近在學(xué)習(xí)three.js,這篇文章屬于系列文章,下面這篇文章主要給大家介紹了關(guān)于Three.js如何繪制字體模型的相關(guān)資料,通過(guò)文中介紹的方法實(shí)現(xiàn)的效果非常的贊,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-09-09
javascript showModalDialog,open取得父窗口的方法
showModalDialog,open取得父窗口的代碼,需要的朋友可以參考下。2010-03-03
微信小程序獲取用戶(hù)頭像昵稱(chēng)組件封裝實(shí)例(最新版)
我們?cè)谶M(jìn)行小程序開(kāi)發(fā)的時(shí)候,往往需要獲取微信用戶(hù)的部分個(gè)人信息,常見(jiàn)的有用戶(hù)頭像,昵稱(chēng)等,下面這篇文章主要給大家介紹了關(guān)于微信小程序獲取用戶(hù)頭像昵稱(chēng)組件封裝的相關(guān)資料,需要的朋友可以參考下2022-12-12
基于JS代碼實(shí)現(xiàn)導(dǎo)航條彈出式懸浮菜單
這篇文章主要介紹了基于JS代碼實(shí)現(xiàn)導(dǎo)航條彈出式懸浮菜單的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
JS實(shí)現(xiàn)對(duì)中文字符串進(jìn)行utf-8的Base64編碼的方法(使其與Java編碼相同)
這篇文章主要介紹了JS實(shí)現(xiàn)對(duì)中文字符串進(jìn)行utf-8的Base64編碼的方法,對(duì)比java的base64編碼程序,分析了javascript實(shí)現(xiàn)base64編碼的相關(guān)技巧,需要的朋友可以參考下2016-06-06
JavaScript獲取字符串實(shí)際長(zhǎng)度(包含中英文)
這篇文章介紹了JavaScript獲取字符串實(shí)際長(zhǎng)度(包含中英文)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06

