JS --- 修改自定义属性

hasAttribute():判断是否有某个自定义属性

getAttribute():获取自定义属性 传入属性名

setAttribute():设置自定义属性 传入要设置的属性名和属性值

removeAttribute(): 传入要删除的属性名

<div id="stu" name="lucy" age="15" height="758">王五</div>
<script type="text/javascript">
    var  oStu = document.getElementById('stu');
    console.log(oStu.hasAttribute('name'));//判断

    var myAttr = oStu.getAttribute('age');//获取
    console.log(myAttr);

    oStu.setAttribute('age','18');//替换
    console.log(oStu);

    oStu.removeAttribute('age');//删除
    console.log(oStu);

    oStu.setAttribute('class','mystyle');
</script>    

猜你喜欢

转载自www.cnblogs.com/zhuoshen/p/9578573.html