HTML - DOMTokenList remove() 方法



HTML DOMTokenList **remove()** 方法用于从 DOMTokenList 中移除参数中指定的一个或多个标记。

语法

domtokenlist.remove(token);

参数

此方法接受如下所示的单个参数。

参数 描述
token 它表示要从 DOMTokenList 中移除的标记的名称。

返回值

此方法不返回任何值。

HTML DOMTokenList 'remove()' 方法示例

以下示例演示了 DOMTokenList remove() 方法的实现。

从元素中移除类

以下示例演示了如何使用 **remove()** 方法从任何元素中移除类。移除的类将移除元素的背景色和文本颜色。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML DOMTokenList remove() Method</title>
    <style>
        .color {
            background-color: #04af2f;
            color: white;
        }
        .font {
            font-size: 40px;
        }
    </style>
</head>
<body>
    <button onclick="fun()">Add</button>
    <button onclick="remove()">Remove</button>
    <p>Hii..</p>
    <p id="remove">Welcome to Tutorials Point...</p>
    <script>
        function fun() {
            let x = document.getElementById("remove").classList;
            x.add("color", "font");

        }
        function remove() {
            let x = document.getElementById("remove").classList;
            x.remove("color");
        }
    </script>
</body>
</html>

从元素中移除多个类

在以下示例中,我们从段落元素中移除多个类。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML DOMTokenList remove() Method</title>
    <style>
        .color {
            background-color: #04af2f;
            color: white;
        }
        .font {
            font-size: 40px;
        }
        .align {
            text-align: center;
        }
    </style>
</head>
<body>
    <button onclick="fun()">Add</button>
    <button onclick="remove()">Remove</button>
    <p>Hii..</p>
    <p id="remove">Welcome to Tutorials Point...</p>
    <script>
        function fun() {
            let x = document.getElementById("remove").classList;
            x.add("color", "font", "align");
        }
        function remove() {
            let x = document.getElementById("remove").classList;
            x.remove("color", "font");
        }
    </script>
</body>
</html>

使用条件语句移除类

在以下示例中,我们使用了条件语句,如果元素具有类 'font',则移除类 'align'。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML DOMTokenList remove() Method</title>
    <style>
        .color {
            background-color: #04af2f;
            color: white;
        }
        .font {
            font-size: 40px;
        }
        .align {
            text-align: center;
        }
    </style>
</head>
<body>
    <button onclick="remove()">Remove</button>
    <p>Hii..</p>
    <p id="remove">Welcome to Tutorials Point...</p>
    <script>
        function remove() {
            let x = document.getElementById("remove").classList;
            x.add("color", "font", "align");
            if (x.contains("font")) {
                x.remove("align");
            }
        }
    </script>
</body>
</html>

支持的浏览器

方法 Chrome Edge Firefox Safari Opera
remove() 是 8 是 12 是 3.6 是 5.1 是 12.1
html_domtokenlist_reference.htm
广告
© . All rights reserved.