JS中的自定义属性

<div id="div1" a="a" data-bbb="bbb">div</div>
<script>
    var oDiv=document.getElementById('div1');
    oDiv.b='b';
    alert(oDiv.a);  //undefined;因为HTML中的自定义属性不能直接获取
    alert(oDiv.b);  //b;通过JS添加的自定义属性能直接获取
    alert(oDiv.getAttribute('a'));  //a;自定义属性可通过getAttribute()获取,但是IE67下有一定的兼容问题,比如获取class,IE67下是使用getAttribute('className'),但其它是getAttribute('class')
    alert(oDiv.dataset.bbb);  //bbb;是HTML5新增自定义属性的方法
</script>
原文地址:https://www.cnblogs.com/3body/p/5417241.html