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追加而不删除


基础教程推荐
- 检查 HTML5 拖放文件类型 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01