jQuery';s replaceWith()是否也删除事件处理程序?

jQuery';s replaceWith()是否也删除事件处理程序?,jquery,Jquery,我已经浏览了jQuery文档,找到了这个简单问题的答案: replaceWith()是否在发动机罩下调用remove()或detach() 我在官方文件方面运气不佳;它被模糊地定义为删除节点。但是测试显示内存泄漏。查看jQuery(2.1.1)的源代码,您会问两个不同的问题 jQuery的replaceWith()是否删除事件处理程序? 对,它是一种内部方法,用于删除元素上的所有数据。由于jQuery事件处理程序存储在元素数据中,因此它们也将被清除 cleanData()还通过调用jQuery.

我已经浏览了jQuery文档,找到了这个简单问题的答案:

replaceWith()
是否在发动机罩下调用
remove()
detach()


我在官方文件方面运气不佳;它被模糊地定义为删除节点。但是测试显示内存泄漏。

查看jQuery(2.1.1)的源代码,您会问两个不同的问题

  • jQuery的
    replaceWith()
    是否删除事件处理程序?

    对,它是一种内部方法,用于删除元素上的所有数据。由于jQuery事件处理程序存储在元素数据中,因此它们也将被清除

    cleanData()
    还通过调用
    jQuery.removeEvent()
    (另一个内部方法),删除附加到元素的事件处理程序,该事件处理程序触发存储在元素数据中的所有事件处理程序的执行

  • 是否在发动机罩下调用
    remove()
    detach()

    它调用
    remove()
    的唯一时间是没有为
    replaceWith()
    提供任何参数;jQuery将其视为调用
    remove()
    而不是
    replaceWith()



  • TL;DR:jQuery将为您清理一切,因此不应存在内存泄漏的风险。

    FYW如果您不希望出现这种行为,请查看jQuery票证上这个讨论不多的线程

    对于那些没有准备好使用代码的人,这里有一个
    replaceWith
    的实现,它使用
    detach
    而不是
    remove

    使用remove()

    old.replacetwith(新)

    使用detach()

    old.before(new.detach()

    1)带有A。替换为(B)它将删除A元素和所有附加的事件。必须声明B的所有事件

    2)在(B)之前使用A.detach()它将删除A元素,但保留事件供以后使用(并且将仅再次应用于A)。必须声明B的所有事件

    这个问题可以通过使用事件传播或事件冒泡来解决,如下代码所示

    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){ 
      $(".rp").click(function(){
        let variable1 = parseInt($(this).text(), 10) + 6;
        $(this).replaceWith("<button class='rp'>"+ variable1 + "</button>");
    
       });
    
       $(".detach").click(function(){
        let variable1 = parseInt($(this).text(), 10) + 6;
        $(this).replaceWith("<button class='detach'>"+ variable1 + "</button>");
    
       });
    
       $("body").on("click", ".detach2",function(){
        let variable1 = parseInt($(this).text(), 10) + 6;
        $(this).replaceWith("<button class='detach2'>"+ variable1 + "</button>");
       });
    
       $("body").on("click", ".detach3",function(){
        let variable1 = parseInt($(this).text(), 10) + 6;
        $(this).before("<button class='detach3'>"+ variable1 + "</button>"). detach();
       });
    
    });
    </script>
    </head>
    <body>
    
    <h1>This is a heading</h1>
    
    <p>with ReplaceWith <button class="rp">1</button></p>
    <p>with before / detach <button class="detach">2</button> </p>
    <p>with ReplaceWith correct event bubbling <button class="detach2">3</button> </p>
    <p>with before / detach and correct event bubbling <button class="detach3">4</button> </p>
    
    </body>
    </html>
    
    
    $(文档).ready(函数(){
    $(“.rp”)。单击(函数(){
    让variable1=parseInt($(this).text(),10)+6;
    $(this.replace为(“+variable1+”);
    });
    $(“.detach”)。单击(函数(){
    让variable1=parseInt($(this).text(),10)+6;
    $(this.replace为(“+variable1+”);
    });
    $(“body”)。在(“click”,“.detach2”,function()上{
    让variable1=parseInt($(this).text(),10)+6;
    $(this.replace为(“+variable1+”);
    });
    $(“body”)。在(“click”,“.detach3”,function()上{
    让variable1=parseInt($(this).text(),10)+6;
    $(this).before(“+variable1+”).detach();
    });
    });
    这是一个标题
    替换为1

    与before/detach 2一起使用

    替换为正确的事件冒泡3

    使用before/detach和correct事件冒泡4

    希望这能帮助某人