JavaScript 参考手册
HTML DOM setAttributeNode() 方法
元素对象
源代码:
点击运行 »
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>蜜蜂教程(mifengjc.com)</title> <style type="text/css"> .democlass { color: red; } </style> </head> <body> <h1>Hello World</h1> <p id="demo">单击按钮来设置标题上面的类属性节点。</p> <button onclick="myFunction()">点我</button> <script> function myFunction() { var atr = document.createAttribute("class"); atr.nodeValue = "democlass"; var h = document.getElementsByTagName("H1")[0]; h.setAttributeNode(atr); }; </script> </body> </html>
运行结果:
点击运行 »