我目前无法在添加新元素之前清除容器div. (当空()时,整个div被擦干净,我不想要这个.我只想删除现有的书架元素.有什么我做错了吗?和/或可能因为“文本/模板“脚本?)
HTML片段
<div class="component">
<ul id="Maincontainer" class="align">
<script type="text/template" id="template">
<li id="bookHolder" class="bookHolder">
<figure class="book">
<!-- Front -->
<ul class="hardcover_front">
<li>
<img src="{{imgurL}}" alt="" width="100%" height="100%">
</li>
<li></li>
</ul>
</figure>
</li>
</script>
</ul>
</div>
JQUERY片段
$.get( "URL",function(data) {
var entry = $(data).find("entry");
entry.each(function() {
var template = $("#template").html();
//$("#Maincontainer").remove(); - >clear whole div,appended elements not show
//$("#Maincontainer").remove("#bookHolder"); -> Div not cleared
$("#Maincontainer").append(template.replace("{{bookName}}",bookName).replace("{{summary}}",summary).replace("{{price}}",price).replace("{{seller}}",artist).replace("{{imgurL}}",image)
.replace("{{href}}",href).replace("{{category}}",category));
});
});
解决方法
您可以使用jQuery的remove方法从DOM中删除元素:
$(".bookHolder").remove();