jQuery——类的添加与删除

添加类:addClass

删除类:removeClass

判断类是否存在:hasClass

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="jquery-1.11.1.js"></script>
    <style>
        div {
            width: 100px;
            height: 100px;
            border: 1px solid #ccc;
            float: left;
        }

        .box1 {
            margin-right: 10px;
        }

        .box2 {
            margin-left: 10px;
        }

        .show {
            background-color: red;
        }
    </style>

    <script src="jquery-1.11.1.js"></script>
    <script>
        $(function () {
            $(".box2").addClass("show");
            $(".box1").removeClass("show");
            console.log($("box1").hasClass("show"));
        });
    </script>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
</html>
原文地址:https://www.cnblogs.com/wuqiuxue/p/8032196.html