<!DOCTYPE html> <html> <style> .democlass {color:red} </style> <body> <h1>HTML DOM Elements</h1> <h2>setAttribute() 方法</h2> <p>将第一个 H1 的 class 属性设置为 "democlass"。</p> <script> const element = document.getElementsByTagName("H1")[0]; element.setAttribute("class", "democlass"); </script> </body> </html>