如何解开.detach()? [英] How to I undo .detach()?

查看:116
本文介绍了如何解开.detach()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JQuery 1.5和下面的代码来分离单击按钮时某个类的li元素。我想知道的是,当再次点击该按钮时,如何将元素添加回页面?

I'm using JQuery 1.5 and the following code to detach li elements w/ a certain class when a button is clicked. What I want to know is, when that button is clicked again, how do I add the element back onto the page?

    <script>
    $("#remove").click(function () {
      $('li.type').fadeOut(300, function() { $(this).detach(); });
    });</script>


推荐答案

问题是: where 在页面上是否要放回元素?例如,如果所有 li 元素都回到< ul id =foo>< / ul> 你可以使用这样的东西:

The question is: where on the page do you want to put the element back? If, for example, all the li elements go back inside <ul id="foo"></ul> you might use something like this:

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 = [];
});

这篇关于如何解开.detach()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆