How can I use jQuery.load to replace a div including the div

前端 未结 13 1633
半阙折子戏
半阙折子戏 2020-11-27 14:40

I have a div with the id of \"secondHeader\" and I want to replace that entire div with another div with the same id of \"secondHeader\" but instead of replacing it , it jus

13条回答
  •  再見小時候
    2020-11-27 15:29

    Final Answer:

    $.fn.loadWith = function(u){var c=$(this);$.get(u,function(d){c.replaceWith(d);});};
    $("#test").loadWith("somelink.html");
    

    jQuery load adds the response INTO the element selected. jQuery's replaceWith REPLACES the selected element.

    start
    $("#curElement").load("somelink.html"); will result in:
    What ever was in somelink.html
    $("#curElement").replaceWith("somelink.html"); will result in: What ever was in somelink.html

    I suggest adding a function to jQuery that does both:

    $.fn.loadWith = function(u){
        var c=$(this);
        $.get(u,function(d){
            c.replaceWith(d);
        });
    };
    $("#test").loadWith("somelink.html");
    

提交回复
热议问题