JS---DOM---自定義屬性引入和移除


總結:在html標簽中添加的自定義屬性, 如果想要獲取這個屬性的值, 需要使用getAttribute("自定義屬性的名字")才能獲取這個屬性的值

  1.   html標簽中有沒有什么自帶的屬性可以存儲成績的----沒有
  2.   本身html標簽沒有這個屬性, 自己(程序員)添加的----自定義屬性---為了存儲一些數據
 
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title></title>
  <style>
    ul {
      list-style-type: none;
      cursor: pointer;
    }
  </style>
</head>

<body>
  <ul>
    <li score="60">張三的數學成績</li>
    <li score="90">趙四的數學成績</li>
    <li score="90">王五的數學成績</li>
    <li score="80">唐七的數學成績</li>
    <li score="50">宋八的數學成績</li>
  </ul>
  <script src="common.js"></script>
  <script>
    var list = document.getElementsByTagName("li");
    for (var i = 0; i < list.length; i++) {
      list[i].onclick = function () {
        // alert(this.score);//彈出undefined 
        alert(this.getAttribute("score"));
      };
    }
  </script>
</body>

 

移除自定義屬性:removeAttribute("屬性的名字")

  <input type="button" value="移除自定義屬性" id="btn" />
  <div id="dv" score="80分" class="cls"></div>
  <script src="common.js"></script>
  <script>
    my$("btn").onclick = function () {
      // my$("dv").removeAttribute("score");
      // my$("dv").className = "";
      //也可以移除元素的自帶的屬性
      my$("dv").removeAttribute("class");
    };
  </script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM