专注前端素材!
源代码:
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>素材8网教程(study.sucai8.cn)</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>
运行结果