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