问题描述
所以我需要从特定类中获取内容并将其放入一个 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);
});
演示
这篇关于Jquery追加而不删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)