修改样式属性

Tutorial: DOM操作 Category: JS Published: 2026-04-07 13:58:26 Views: 20 Likes: 0 Comments: 0
<!DOCTYPE html>
<html lang="en">
  <head>
    <style>
      div {
        width: 200px;
        height: 200px;
        background-color: pink;
      }
    </style>
  </head>

  <body>
    <div></div>

    <script>
      // 1. 获取元素
      var div = document.querySelector("div");

      // 2. 注册事件 处理程序
      div.onclick = function () {
        // div.style里面的属性 采取驼峰命名法
        this.style.backgroundColor = "purple";
        this.style.width = "250px";
      };
    </script>
  </body>
</html>