目录
元素的属性操作
一、获取元素属性 getAttributte('属性名‘)
代码如下(示例):
//html代码:
<div id="box" class="box" name_1="name">
//js代码:
var box = document.getElementById('box')
console.log(box.getAttribute('id'));
console.log(box.getAttribute('class'));
console.log(box.getAttribute('name_1'));
二、设置元素属性 setAttribute(‘属性名’,‘属性值’')
代码如下(示例):
//html代码:
<button id="btn">按钮</button>
//js代码:
document.getElementById('btn').onclick = function () {
box.setAttribute('class', 'box_1')
}
三、移除元素属性 removeAttribute(‘属性名’)
代码如下(示例):
//html代码:
<button id="remove">移除</button>
//js代码:
document.getElementById("remove").onclick = function () {
console.log('测试');
box.removeAttribute('id')
box.removeAttribute('name_1')
}