jQuery addClass()、removeClass()、hasClass()

122 阅读1分钟
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    div {
      width: 100px;
      height: 100px;
      background-color: pink;
    }
    .current {
      background-color: red;
    }
  </style>
  <script src="jquery-3.4.1.js"></script>
  <script>
    $(function () {
      $('button').click(function () {
        // 添加类
        $('div').addClass('current');

        // 删除类
        // $('div').removeClass('current');

        // 是否存在该类
        // console.log($('div').hasClass('current'));
      });
    })
  </script>
</head>
<body>
  <button>变色</button>
  <div></div>
</body>
</html>