x
 
<!DOCTYPE html>
<html>
<style>
.democlass {
  color: red;
}
</style>
<body>
<h1 id="myH1">Element 对象</h1>
<h2>setAttribute() 方法</h2>
<p>点击“添加类”为 h1 元素添加 class 属性:</p>
<button onclick="myFunction()">添加类</button>
<script>
function myFunction() {
  document.getElementById("myH1").setAttribute("class", "democlass"); 
}
</script>
</body>
</html>