Jquery附加而不删除 [英] Jquery appending without removing

查看:65
本文介绍了Jquery附加而不删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需要从特定类中获取内容并将其放在div中,我使用追加...我的问题是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而不从 .open 类中删除它?

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')。点击函数,因为我无法检测 .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);
});

Demo

这篇关于Jquery附加而不删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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