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