<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .box{width: 200px;height: 200px;background: yellowgreen}
        .page{color: red}
        .btn{font-size: 20px;}
    </style>
</head>
<body>

<div class="box">这是测试</div>

<script>
var box=document.getElementsByClassName("box")[0];

        box.className=box.className+" "+"page";//增加类名
        box.classList.add("btn");//增加类名
        box.classList.remove("btn");//移除类名
        box.classList.item(1);//根据索引找类名
        console.log(box.className);
        console.log(box.classList.item(1))

</script>
</body>
</html>

原生js增加,移除类名

原生js增加,移除类名

相关文章:

  • 2022-01-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
  • 2022-12-23
  • 2021-12-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
  • 2022-12-23
相关资源
相似解决方案