Javascript:通过 classList 操作类控制CSS

为了解决className 容易覆盖以前的类名的问题,可以通过classList方式追加和删除类名。

语法形式:

// 追加一个类
元素.classList.add('类名')
// 删除一个类
元素.classList.remove('类名')
// 切换一个类:有这个类就删掉,没有这个类就加上
元素.classList.toggle('类名')

示例:增加一个类

<!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>
    .box {
      width: 200px;
      height: 200px;
      color: #333;
    }

    .active {
      color: red;
      background-color: pink;
    }
  </style>
</head>

<body>
  <div class="box">文字</div>
  <script>
    // 通过classList来增加一个类
    // 1. 获取元素
    const box = document.querySelector('.box')
    // 2. 修改样式
    box.classList.add('active')
  </script>
</body>

</html>

展示效果:
在这里插入图片描述

在浏览器工具中查看,现在div元素上有两个类:
在这里插入图片描述

示例:移除类

<!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>
    .box {
      width: 200px;
      height: 200px;
      color: #333;
    }

    .active {
      color: red;
      background-color: pink;
    }
  </style>
</head>

<body>
  <div class="box">文字</div>
  <script>
    // 1. 获取元素
    const box = document.querySelector('.box')
    // 删除类
    box.classList.remove('box')
  </script>
</body>

</html>

在浏览器开发者工具中查看,类被删除了:
在这里插入图片描述

切换类:以前没有这个类,就加上

下面示例中,div元素以前没有active这个类,执行toggle(‘active’)以后,就增加了active类:

<!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>
    .box {
      width: 200px;
      height: 200px;
      color: #333;
    }

    .active {
      color: red;
      background-color: pink;
    }
  </style>
</head>

<body>
  <div class="box">文字</div>
  <script>
     // 1. 获取元素
    const box = document.querySelector('.box')
    // 切换类, toggle,有这个类就删掉,没有这个类就就加上
    box.classList.toggle('active')
  </script>
</body>

</html>

浏览器开发者工具中查看,增加了active类:
在这里插入图片描述

切换类:以前有这个类,就删除

下面示例中,div元素以前有active这个类,执行toggle(‘active’)以后,就删除了active类:

<!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>
    .box {
      width: 200px;
      height: 200px;
      color: #333;
    }

    .active {
      color: red;
      background-color: pink;
    }
  </style>
</head>

<body>
  <div class="box active">文字</div>
  <script>
    // 1. 获取元素
    const box = document.querySelector('.box')
    // 2. 修改样式
    // 切换类, toggle,有这个类就删掉,没有这个类就就加上
    box.classList.toggle('active')
  </script>
</body>

</html>

在浏览器开发中查看,删除了active类:
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值