我们如何在不使用jquery的情况下使用javascript创建和删除属性节点 createAttribute,removeAttribute
答案 0 :(得分:0)
function myFunction() {
var h1=document.getElementById("ik");
var att = document.createAttribute("class");
att.value = "democlass";
h1.setAttributeNode(att);
}
function myFunction1() {
var h1=document.getElementById("ik");
var att = h1.getAttributeNode("class");
h1.removeAttributeNode(att);
}
.democlass {
color: red;
}
<!DOCTYPE html>
<html>
<body>
<h1 id="ik">Hello World</h1>
<p>Click the button to create a "class" attribute with the value "democlass" and insert it to the H1 element above.</p>
<button onclick="myFunction()">Try it</button>
<button onclick="myFunction1()">Try it</button>
</body>
</html>