Jquery: How to clear an element before appending new content?

前端 未结 4 1787
情话喂你
情话喂你 2020-12-30 19:34

This is my jquery code

 $.ajax({
   url: \"PopUpProductDetails.aspx\",
            cache: false
     }).done(function (html) {
     $(\"#dialog\").append(htm         


        
相关标签:
4条回答
  • 2020-12-30 19:54

    Inside the function clear the dialog first and then fill up with the content

    $.ajax({
       url: "PopUpProductDetails.aspx",
                cache: false
         }).done(function (html) {
         $("#dialog").html("");
         $("#dialog").html(html);
     });
    
    0 讨论(0)
  • 2020-12-30 19:55

    You can use .empty() first, then use .append() in one line :)

    like this:

    $.ajax({
      url: "PopUpProductDetails.aspx",
            cache: false
        }).done(function (html) {
        $("#dialog").empty().append(html);
    });
    
    0 讨论(0)
  • 2020-12-30 19:59

    in case you need to clear the dialog first use .empty() which is faster than .html("")

    see Jquery Difference .html("") vs .empty()

    0 讨论(0)
  • 2020-12-30 20:15

    .append() appends html to the end of existing html string, use .html() to replace what's currently inside of #dialog.

    0 讨论(0)
提交回复
热议问题