自定义属性设置

2019-10-09  本文已影响0人  王远清orz
<body>
  <div id="box" age="18" name="1">张三</div>
  <script>
    var box = document.getElementById('box');
    console.log(box.id); //box
    console.log(box.age); //undefined
    console.log(box.name); //undefined

    // 获取自定义属性的值  getAttribute
    console.log(box.getAttribute('age'));

    //设置自定义属性
    box.setAttribute('age','20');
    console.log(box.getAttribute('age'));

    //移除自定义属性
    box.removeAttribute('age','name');
  </script>
</body>
上一篇 下一篇

猜你喜欢

热点阅读