Version: Next
Element对象
获取
通过document
对象获取
方法
removeAttribute();
setAttribute();
<body>
<a>点我试一试</a>
<input type="button" value="给a标签设置属性" id="btn_set"/>
<input type="button" value="删除属性" id="btn_remove"/>
<script>
//1.获取a标签
var btn1 = document.getElementById("btn_set");
var btn2 = document.getElementById("btn_remove");
btn1.onclick = function () {
var element_a = document.getElementsByTagName("a")[0];
element_a.setAttribute("href", "https://www.baidu.com");
}
btn2.onclick = function () {
var element_a = document.getElementsByTagName("a")[0];
element_a.removeAttribute("href");
}
</script>
</body>