How to I undo .detach()?

前端 未结 2 1913
梦谈多话
梦谈多话 2021-01-18 18:44

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, h

相关标签:
2条回答
  • 2021-01-18 19:24

    here u can't for loop.

    var demo;
    $('li.type').fadeOut(300, function() {
         demo = $(this).detach();
    });
    
    $('#replace').click(function() {
       $("ul#foo").append(demo);
    });
    
    0 讨论(0)
  • 2021-01-18 19:29

    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 = [];
    });
    
    0 讨论(0)
提交回复
热议问题