JavaScript中property和attribute的区别详细介绍
1.定义
Property:属性,所有的HTML元素都由HTMLElement类型表示,HTMLElement类型直接继承自Element并添加了一些属性,添加的这些属性分别对应于每个HTML元素都有下面的这5个标准特性:id,title,lang,dir,className。DOM节点是一个对象,因此,他可以和其他的JavaScript对象一样添加自定义的属性以及方法。property的值可以是任何的数据类型,对大小写敏感,自定义的property不会出现在html代码中,只存在js中。
Attribute:特性,区别于property,attribute只能是字符串,大小写不敏感,出现在innerHTML中,通过类数组attributes可以罗列所有的attribute。
2.相同之处
标准的DOMproperties与attributes是同步的。公认的(非自定义的)特性会被以属性的形式添加到DOM对象中。如,id,align,style等,这时候操作property或者使用操作特性的DOM方法如getAttribute()都可以操作属性。不过传递给getAttribute()的特性名与实际的特性名相同。因此对于class的特性值获取的时候要传入“class”。
3.不同之处
1).对于有些标准的特性的操作,getAttribute与点号(.)获取的值存在差异性。如href,src,value,style,onclick等事件处理程序。
2).href:getAttribute获取的是href的实际值,而点号获取的是完整的url,存在浏览器差异。
<script> vara =document.body.children[0] a.href='/' alert('attribute:'+a.getAttribute('href'))//'/' alert('property:'+a.href) //IE:'/',others:fullURL </script>
src的值的获取类似href,不过IE也会返回fullURL;
value值同样存在一些‘one-way'(单向)同步的内置属性。
例如,input.value从attribute中同步(即property从attribute中获得同步)
<inputtype="text"value="markup"> <script> varinput=document.body.children[0]; input.setAttribute('value','new'); alert(input.value);//'new',input.valuechanged alert(input.getAtrribute(value));//'new' </script>
但是attribute不能从property中获得同步:
<inputtype="text"value="markup"> <script> varinput=document.body.children[0]; input.value='new'; alert(input.getAttribute('value')); //'markup',notchanged! </script>
getAttribute获取的是初始值,而点号获取的是初始值或者.value修改后的值,例如当访问者输入了某些字符后,'value'attribute在property更新后维持了原始值。原始值可以用来检验input是否变化,或者重置它。
对于style和onclick等事件处理程序,getAttribute方法访问时会返回字符串,而点号返回的是相应的对象和事件处理函数。
对于input中的checked属性
<script> varinput =document.body.children[0] alert(input.checked)//true alert(input.getAttribute('checked'))//emptystring </script>
getAttribute获取的是你是实际设置的值。而点号返回的是布尔值。
浏览器兼容性上的差别
1.在IE<9的浏览器中,可以用点号和getAttribute在相互之间访问自定义属性。
2.IE<8(包括IE8种的IE7兼容模式),property和attribute相同。因为attribute对大小写不敏感,在这种情况下,用getAttribute访问特性的时候,浏览器会选择第一次出现的值。
document.body.abba=1//assignproperty(nowcanreaditbygetAttribute) document.body.ABBA=5//assignpropertywithanothercase //mustgetapropertynamed'ABba'incase-insensitiveway. alert(document.body.getAttribute('ABba'))//1
优先选择property
在实际应用中,98%的DOM操作都是使用properties。
只有两种情形需要使用attributes
1.自定义HTMLattributes,因为它并不同步到DOMproperty。
2.访问内置的HTMLattributes,这些attribute不能从property同步过来。例如INPUT标签的value值。