[javaquery知识小结]jQuery提供了三种删除节点的方法

本文介绍了jQuery中三种常用元素操作方法:remove(), empty() 和 detach() 的使用方式及区别。通过实例展示了如何利用这些方法来删除或清空DOM元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

remove()

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="jquery-3.3.1.min.js"></script>
    <script>
//属性选择器
            $("input[type=button]").click(function () {
//直接调用remove()
                $(".top>div:last>img").remove();
            });
        });


    </script>
</head>
<body>
    <div class="top">
        <div>
            <ul>
                <li>1</li>
                <li>2</li>
            </ul>
        </div>
        <div>
            <img src="img/IMG_0860.JPG">
        </div>

    </div>
    <div class="button">
        <form method="POST">
            <input type="button" value="删除">
        </form>
    </div>
</body>

</html>
    哪个节点学要被删除,则哪个节点去调用remove()方法

Empty()

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="jq/jquery-3.3.1.min.js"></script>
    <script>
        $(document).ready(function () {
            //选择包含type选择器包含bu的节点
            $("input[type*=bu]").bind("click", function () {
                // alert("132");
                $(".top").empty();
            });
        });

    </script>
</head>

<body>
    <div class="top">
        <p>我是黄小涛</p>
    </div>
    <div class="button">
        <form action="" method="POST">
            <input type="button" value="清空">
        </form>
    </div>
</body>

</html>
谁要删除文本的内容,谁就去调用empty()方法,这个方法会删除匹配的元素集合中所有的子节点。(包括文本内容)

detach()

    从DOM中删除所有匹配的元素。
    这个方法不会把匹配的元素从jQuery对象中删除,因而可以在将来再使用这些匹配的元素。与remove()不同的是,所有绑定的事件、附加的数据等都会保留下来。
$(this).detach("selector") //可以传入选择器进行二次筛选
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你到底奶不奶我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值