jS获取元素自定义属性

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div class="test" value="222">222</div>
    <script>
      var obj = document.querySelector(".test");
      // obj.setAttribute("属性", "值");  // 设置值
      // obj.getAttribute("属性"); // 获取值
      console.log(obj.getAttribute("value"));
      obj.setAttribute("value", "444");
    </script>
  </body>
</html>

 

posted @ 2021-12-14 15:40  花开丶良人归  阅读(789)  评论(0编辑  收藏  举报