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

论坛 期权论坛 脚本     
匿名网站用户   2020-12-21 09:35   31   0

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") //可以传入选择器进行二次筛选
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP