dom中attribute和property的区别

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. 相同之处

标准的 DOM properties 与 attributes 是同步的。公认的(非自定义的)特性会被以属性的形式添加到DOM对象中。如,id,align,style等,这时候操作property或者使用操作特性的DOM方法如getAttribute()都可以操作属性。不过传递给getAttribute()的特性名与实际的特性名相同。因此对于class的特性值获取的时候要传入“class”。

3. 不同之处

  • 对于有些标准的特性的操作,getAttribute与点号(.)获取的值存在差异性。如href,src,value,style,onclick等事件处理程序。
  • href:getAttribute获取的是href的实际值,而点号获取的是完整的url,存在浏览器差异。
?
1
2
3
4
5
6
<script>
       var a  = document.body.children[0]
       a.href = '/'
       alert( 'attribute:' + a.getAttribute( 'href' ) ) // '/'
       alert( 'property:' + a.href )  // IE: '/', others: full URL
   </script>

src的值的获取类似href,不过IE也会返回full URL;

value值同样存在一些 ‘one-way’(单向)同步的内置属性。

例如,input.value 从 attribute 中同步(即 property 从 attribute 中获得同步)

?
1
2
3
4
5
6
7
<input type= "text" value= "markup" >
   <script>
        var input = document.body.children[0];
        input.setAttribute( 'value' , 'new' );
        alert( input.value ); // 'new', input.value changed
        alert( input.getAtrribute(value) ); // 'new'
   </script>

但是 attribute 不能从 property 中获得同步:

?
1
2
3
4
5
6
<input type= "text" value= "markup" >
    <script>
         var input = document.body.children[0];
         input.value = 'new' ;
         alert(input.getAttribute( 'value' ));  // 'markup', not changed!
   </script>

getAttribute获取的是初始值,而点号获取的是初始值或者.value修改后的值,例如当访问者输入了某些字符后,’value’ attribute 在 property 更新后维持了原始值。原始值可以用来检验 input 是否变化,或者重置它。

对于style和onclick等事件处理程序,getAttribute方法访问时会返回字符串,而点号返回的是相应的对象和事件处理函数。

对于input中的checked属性,

?
1
2
3
4
5
<script>
     var input  = document.body.children[0]
     alert( input.checked ) // true
     alert( input.getAttribute( 'checked' ) ) // empty string
   </script>

getAttribute获取的是你是实际设置的值。而点号返回的是布尔值。

浏览器兼容性上的差别

  • 在IE<9的浏览器中,可以用点号和getAttribute在相互之间访问自定义属性。
  • IE<8(包括IE8种的IE7兼容模式),property和attribute相同。因为attribute对大小写不敏感,在这种情况下,用getAttribute访问特性的时候,浏览器会选择第一次出现的值。

    ?
    1
    2
    3
    4
    document.body.abba = 1 // assign property (now can read it by getAttribute)
    document.body.ABBA = 5 // assign property with another case
    // must get a property named 'ABba' in case-insensitive way.
    alert( document.body.getAttribute( 'ABba' ) ) // 1

    优先选择property

    在实际应用中,98%的 DOM 操作都是使用 properties。

    只有两种情形需要使用attributes

    • 自定义 HTML attributes,因为它并不同步到DOM property。
    • 访问内置的 HTML attributes,这些 attribute 不能从 property 同步过来。例如 INPUT标签的value值。

    参考资料: http://javascript.info/tutorial/attributes-and-custom-properties

  • http://www.liyao.name/2013/09/differences-between-property-and-attribute-in-javascript.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值