Wonderjar's Blog

操作Dom

2019-05-05

修改一个node的class

1
2
3
4
5
6
7
8
9
10
11
document.getElementById("MyElement").className = 'MyClass';

document.getElementById("MyElement").classList.add('MyClass');

document.getElementById("MyElement").setAttribute('class', 'MyClass'')

document.getElementById("MyElement").getAttributeNode('class').value = 'MyClass'

const attr = document.createAttribute("class");
attr.value = "test";
document.getElementById("MyElement").setAttributeNode(attr);