style对象的使用,当点击时背景颜色变为黄色

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <!--当点击时背景颜色变为黄色-->
    <script>
          function changeColor(id){
              id.style.backgroundColor='yellow';
          }
    </script>
</head>
<body>
      <p onclick="changeColor(this)">JavaScript事件处理</p>
</body>
</html>

未点击

点击后

猜你喜欢

转载自blog.csdn.net/weixin_42044486/article/details/84036798