zoukankan      html  css  js  c++  java
  • jQuery基础:remove()与 detach()区别

    1、detach()

    • detach() 方法移除被选元素,包括所有文本和子节点。
    • 这个方法会保留 jQuery 对象中的匹配的元素,因而可以在将来再使用这些匹配的元素。
    • detach() 会保留所有绑定的事件、附加的数据,这一点与 remove() 不同。
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style>
            p{
                margin: 6px;
                background: yellow;
            }
            p.off{
                background: red;
            }
        </style>
    </head>
    <body>
        <p>hello</p>
        how are
        <p>you?</p>
        <button>按钮</button>
    </body>
    <script src="libs/jquery-1.8.3.min.js"></script>
    <script type="text/javascript">
        $(function(){
            $("p").click(function(){
                $(this).toggleClass("off");
            })
    
            var p;
            $("button").click(function(){
                if(p){
                    p.appendTo("body");
                    p = null;
                }else{
                    p = $("p").detach();
                    console.log(p);
                }
            })
        });
    </script>
    </html>

    2、remove()

    • 将匹配元素集合从DOM中删除。
    • 同时移除元素上的事件及 jQuery 数据。
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style>
            p{
                margin: 6px;
                background: yellow;
            }
            p.off{
                background: red;
            }
        </style>
    </head>
    <body>
        <p>hello</p>
        how are
        <p>you?</p>
        <button>按钮</button>
    </body>
    <script src="libs/jquery-1.8.3.min.js"></script>
    <script type="text/javascript">
        $(function(){
            $("p").click(function(){
                $(this).toggleClass("off");
            })
    
            var p;
            $("button").click(function(){
                if(p){
                    p.appendTo("body");
                    p = null;
                }else{
                    p = $("p").remove();
                    console.log(p);
                }
            });
    
            //移除 =》加上,点击没反应,绑定的事件失效
        });
    </script>
    </html>

    3、empty():移除匹配元素的所有子节点

    4、unwrap():将匹配元素集合的父级元素删除,保留自身(和兄弟元素,如果存在)在原来的位置。

  • 相关阅读:
    arcgis要素折点转点
    arcgis问题数据判断
    arcgis根据位置信息查找一个点周围的线(根据交点,查找)
    GIS算法-最短路径-连通性-网络分析-路径规划
    Arcgis直连SQLServer数据库,通过REST修改数据ArcMap中更新数据库数据不更新,数据不统一
    None和NULL
    ArcPy属性查询
    WGS84转gcj02
    ArcGIS Server跨域
    MySQL中数据类型宽度有什么用, INT(11)有什么意义?
  • 原文地址:https://www.cnblogs.com/gao-xiong/p/5933284.html
Copyright © 2011-2022 走看看