Jquery追加而不删除

2022-01-22 00:00:00 append jquery javascript

所以我需要从特定类中获取内容并将其放入一个 div 中,我使用 append 来......我的问题是 append 删除了我附加的项目,我需要它留在那里,这是我的代码:

So I need to grab content from a specific class and put it in a div, which I use append for...my issue is that append removes the item I append, and I need it to stay there, Here is my code:

$(document).ready(function(){
    var $content = $('#popupcontent');
    var $window = $('#popupwindow');
    $('.open').click(function(){
        //alert('runnning');
        var a = $(this).contents('span');
        $content.append(a);
        $window.fadeIn(300);
    });
    $('.close').click(function(){
        //alert('running');
            var a = $content.contents('span');
        $window.fadeOut(300);
        $('#popupcontent span').remove();
    });
});

那么如何在点击时从每个 .open span#popupcontents id 获取内容而不将其从 中删除.打开类?

So how can I get the content, when clicked, from each .open span to the #popupcontents id without removing it from the .open class?

向您展示我的意思:JSFIDDLE

注意:第二次单击链接时,它不会附加任何内容,因为该内容已从该类中删除,这不是我想要的

NOTE: the second time you click a link, it wont append any content because that content has been removed from that class, which is not what I want

注意 2:我不能简单地在 $('.close').click 函数中仅使用 append 而不是 remove,因为我无法检测到内容来自 .open 类的哪个实例.

NOTE2: I cannot simply just append instead of remove in the $('.close').click function because I cannot detect which instance of the .open class the content came from.

推荐答案

需要克隆元素并追加克隆:

You need to clone the element and append the clone:

$('.open').click(function(){
    //alert('runnning');
    var a = $(this).contents('span');
    $content.append(a.clone());
    $window.fadeIn(300);
});

演示

相关文章