Jquery appending without removing(Jquery追加而不删除)
问题描述
所以我需要从特定类中获取内容并将其放入一个 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追加而不删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Jquery追加而不删除


- addEventListener 在 IE 11 中不起作用 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Fetch API 如何获取响应体? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 400或500级别的HTTP响应 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01