jQuery: does using .html(html) replace current content
When I do something like:
$.post("update.php", {page: $(this).attr('data-page')}, function(success){
$("#dialog").html(html);
}
});
and I have a :
<div id="dialog" style="display: block;">
//a bunch of html in here, initially it's not empty
</di开发者_如何学编程v>
Then will that $("#dialog").html(html);
replace all the bunch of html that initially we have or does it append to it?
.html() will replace the content.
Use .append() to add to the existing content.
It replaces the content. If you want to append it, you can call ".append()":
$('#dialog').append(html);
It will replace it. Note that you should write:
$("#dialog").html(success);
if you want to append the data, use:
$("#dialog").append(success);
Simple, isn't it?
Whatever is in your success (hopefully html) should be appended:
$.post("update.php", {page: $(this).attr('data-page')}, function(success){
$("#dialog").append(success);
}
});
Yes, that existing content will be replaced.
It will replace all the html.
To append use:
$("#dialog").append(html);
It will overwrite the contents of the #dialog
element with your html
parameter. If you just want to add it to the end, use the append method: append(html)
.
from jQuery doc: When .html() is used to set an element's content, any content that was in that element is completely replaced by the new Content.
So it replaces old content
精彩评论