我正在使用JQuery 1.5和以下代码在单击按钮时分离具有某个类的li元素。我想知道的是,当再次点击该按钮时,如何将该元素添加回页面?
<script>
$("#remove").click(function () {
$('li.type').fadeOut(300, function() { $(this).detach(); });
});</script>
答案 0 :(得分:5)
问题是:页面上的 是否要将元素放回去?例如,如果所有li
元素都返回<ul id="foo"></ul>
内,您可能会使用以下内容:
var items = [];
$('li.type').fadeOut(300, function() {
items.push( $(this).detach() );
});
$('#replace').click(function() {
for(var i = 0; i < items.length; i++) {
$("ul#foo").append(items[i]);
}
items = [];
});
答案 1 :(得分:1)
这里你不能循环。
var demo;
$('li.type').fadeOut(300, function() {
demo = $(this).detach();
});
$('#replace').click(function() {
$("ul#foo").append(demo);
});