简体   繁体   English

删除.append()以将元素返回到其原始状态

[英]remove an .append() to return an element to it's original state

I'm loading up a ul for a image slider but I need to remove the images ( ul ) and revert back to the original state (before javascript manipulations) when the slider is closed because I will later need to reload the ul for another set of different images. 我正在为图像滑块加载ul ,但是我需要移除图像( ul )并在关闭滑块时恢复到原始状态(在执行javascript操作之前),因为以后需要为另一组重新加载ul不同的图像。

Here is how I'm loading the slider. 这是我加载滑块的方式。

 var get_images = [ "show_1.jpg", "show_2.jpg", "show_3.jpg", "show_4.jpg"];

 $.each(get_images, function(i,img){
 $('#container ul').append('<li><a href="#" class="thumbnail"><img src="'+img+'"/></a></li>'); 
});

I think this might be what you are looking for: 我认为这可能是您要寻找的:

var get_images = ["show_1.jpg", "show_2.jpg", "show_3.jpg", "show_4.jpg"];
var original = $('#container ul').html();
$.each(get_images, function (i, img) {
    $('#container ul').append('<li><a href="#" class="thumbnail"><img src="' + img + '"/></a></li>');
});
$("#revert").click( function() {
    $('#container ul').html(original);
});

The revert button is for demonstration purposes. 还原按钮用于演示目的。 The idea is basically that you save your original content so that you can revert back to it later if needed. 基本上,您的想法是保存原始内容,以便以后在需要时可以还原到原始内容。

Fiddle 小提琴

如果ul最初为空,则可以使用.empty()将其恢复为该状态。

$('#container ul').empty()

How about getting the last one you added and deleting it like this.: 如何获得添加的最后一个,然后像这样删除它:

 $('#container ul li:last').remove();

If you have added 3, then repeat that line 3 times. 如果添加了3,则重复该行3次。 Each one gets the last added element and removes it. 每个人都获取最后添加的元素并将其删除。


Another way 其他方式

You can also keep track of what is added with: 您还可以跟踪添加的内容:

var addedList = [];

$.each(get_images, function(i,img){
    var added = $.parseHTML('<li><a href="#" class="thumbnail"><img src="'+img+'"/></a></li>');
    $('#container ul').append(added);
    addedList.push(added);
});

Delete with something like: 用以下方式删除:

for (var i=0; i < addedList.length; ++i) {
    $(addedList[i]).remove();
}
addedList = [];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM