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>    

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM