运行 ❯
获取您的
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <style> .democlass {color: red;} </style> <body> <h1>The Document Object</h1> <h2>The createAttribute() and setAttributeNode() Methods</h2> <p>Click "Add" to create a class attribute and add it to first h1 element.</p> <button onclick="myFunction()">Add</button> <script> function myFunction() { // Create a class attribute: const att = document.createAttribute("class"); // Set a value of the class attribute att.value = "democlass"; // Add the class attribute to the first h1; document.getElementsByTagName("h1")[0].setAttributeNode(att); } </script> </body> </html>