ZetCode

JavaScript classList.remove

最后修改于 2025 年 4 月 2 日

在本文中,我们将探讨 JavaScript 中的 classList.remove 方法。此方法对于动态 CSS 类操作至关重要,允许开发人员以编程方式从 DOM 元素中移除类。

基本定义

classList.remove 方法会从元素的 class 属性中移除一个或多个类名。它是 classList 属性返回的 DOMTokenList 接口的一部分。

此方法提供了一种干净的方式来操作 CSS 类,而无需直接处理 className 字符串。它会自动处理空格和重复项,使其比字符串操作更安全。

移除单个类

此示例演示了如何从元素中移除单个类。

index.html
<!DOCTYPE html>
<html>
<head>
    <title>Remove Single Class</title>
    <style>
        .highlight {
            background-color: yellow;
            font-weight: bold;
        }
    </style>
</head>
<body>

<p id="text" class="highlight">This text will lose its highlight.</p>
<button onclick="removeHighlight()">Remove Highlight</button>

<script>
    function removeHighlight() {
        const textElement = document.getElementById('text');
        textElement.classList.remove('highlight');
    }
</script>

</body>
</html>

在此示例中,我们有一个带有 'highlight' 类的段落,该类为其提供了黄色背景。按钮单击会触发使用 classList.remove 移除此类的操作。

该方法将类名作为字符串参数。如果元素上不存在该类,该方法将静默失败而不会抛出错误。

移除多个类

此示例展示了如何一次性从元素中移除多个类。

index.html
<!DOCTYPE html>
<html>
<head>
    <title>Remove Multiple Classes</title>
    <style>
        .big {
            font-size: 2em;
        }
        .red {
            color: red;
        }
        .border {
            border: 2px solid black;
        }
    </style>
</head>
<body>

<div id="box" class="big red border">Styled Box</div>
<button onclick="stripStyles()">Remove Styles</button>

<script>
    function stripStyles() {
        const box = document.getElementById('box');
        box.classList.remove('big', 'red', 'border');
    }
</script>

</body>
</html>

这里我们有一个具有三个类(应用不同样式)的 div。按钮单击通过将多个参数传递给 classList.remove 来同时移除所有三个类。

每个参数代表一个要移除的类。该方法按顺序处理它们,并且不存在的类将被简单地忽略。这比多次调用 remove 更有效。

条件类移除

此示例演示了仅在类存在于元素上时才移除该类。

index.html
<!DOCTYPE html>
<html>
<head>
    <title>Conditional Removal</title>
    <style>
        .active {
            background-color: lightgreen;
        }
    </style>
</head>
<body>

<div id="panel">Content Panel</div>
<button onclick="toggleActive()">Toggle Active</button>

<script>
    function toggleActive() {
        const panel = document.getElementById('panel');
        
        if (panel.classList.contains('active')) {
            panel.classList.remove('active');
        } else {
            panel.classList.add('active');
        }
    }
</script>

</body>
</html>

此示例显示了切换类的常见模式。我们首先使用 classList.contains 检查类是否存在,然后如果存在则移除它。

此方法可确保我们仅在需要时才移除该类。此处也可以使用 classList.toggle 方法来实现更简洁的代码。

移除动画后的类

此示例展示了如何在动画完成后移除动画类。

index.html
<!DOCTYPE html>
<html>
<head>
    <title>Animation Class Removal</title>
    <style>
        .animate {
            animation: fadeIn 1s;
        }
        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }
    </style>
</head>
<body>

<div id="message">Hello, World!</div>
<button onclick="animateMessage()">Animate</button>

<script>
    function animateMessage() {
        const msg = document.getElementById('message');
        msg.classList.add('animate');
        
        msg.addEventListener('animationend', function() {
            msg.classList.remove('animate');
        });
    }
</script>

</body>
</html>

这里我们添加了一个动画类来触发淡入效果,然后通过监听 animationend 事件在动画完成后将其移除。

这种模式可以防止动画类不必要地持久存在,并允许动画被重新触发。它是处理一次性动画的干净方法。

从多个元素中移除类

此示例演示了如何一次性从多个元素中移除类。

index.html
<!DOCTYPE html>
<html>
<head>
    <title>Multiple Elements</title>
    <style>
        .selected {
            outline: 2px solid blue;
        }
    </style>
</head>
<body>

<div class="item selected">Item 1</div>
<div class="item selected">Item 2</div>
<div class="item selected">Item 3</div>
<button onclick="clearSelection()">Clear Selection</button>

<script>
    function clearSelection() {
        const items = document.querySelectorAll('.item');
        
        items.forEach(item => {
            item.classList.remove('selected');
        });
    }
</script>

</body>
</html>

在此示例中,我们选择所有带有 'item' 类的元素,并从每个元素中移除 'selected' 类。这展示了元素的批量处理。

我们使用 querySelectorAll 获取 NodeList,然后使用 forEach 进行迭代。classList.remove 方法在集合中的每个元素上以相同方式工作。

来源

MDN classList 文档

在本文中,我们展示了如何在 JavaScript 中使用 classList.remove。此方法对于现代 Web 开发中的动态 CSS 类操作至关重要。

作者

我叫 Jan Bodnar,我是一名充满热情的程序员,拥有丰富的编程经验。我从 2007 年开始撰写编程文章。迄今为止,我已撰写了 1,400 多篇文章和 8 本电子书。我在教授编程方面拥有十多年的经验。

列出 所有 JS DOM 教程