编辑下面的代码:【加编码】
<html> <head> <style type="text/css"> .democlass{ color:red; } </style> </head> <body> <h1>Hello World</h1> <p id="demo">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> <script> function myFunction() { var h1=document.getElementsByTagName("H1")[0]; var att=document.createAttribute("class"); att.value="democlass"; h1.setAttributeNode(att); } </script> </body> </html>
结果: 【此窗口】 帮助?
Try it Yourself - © 自强学堂