[英]How to clone an element and append to a specific div?
I have a page that has a couple image thumbnails that when clicked on they get cloned and appended into another element. 我有一个页面,其中包含几个图像缩略图,单击它们会被克隆并附加到另一个元素中。 The issue that I'm running in to is that all of the elements that I need to appendTo
all have the same class. 我遇到的问题是,我需要appendTo
所有元素都具有相同的类。 So if I click one thumbnail it gets appended to every single element that has the class view_full_img
. 因此,如果我单击一个缩略图,它将被附加到具有view_full_img
类的每个元素上。 Here is the HTML that I have to work with. 这是我必须使用的HTML。 Simply targeting these with different classes is not going to work for me, this HTML is generated by Drupal. 简单地用不同的类来定位这些对象对我来说是行不通的,该HTML由Drupal生成。
<div class="view_full_img"></div>
<div class="thumbs">
<div class="img blue"></div>
<div class="img green"></div>
<div class="img yellow"></div>
</div>
<div class="view_full_img"></div>
<div class="thumbs">
<div class="img red"></div>
<div class="img purple"></div>
<div class="img orange"></div>
</div>
Here's my jQuery.. 这是我的jQuery ..
$(".thumbs div").click(function(){
$(".view_full_img div").remove();
$(this).clone().appendTo(".view_full_img");
});
What I want to have happen is that when I click one of the .thumbs
divs I want it to get cloned into the .view_full_img
div that is directly above it. 我想发生的事情是,当我单击.thumbs
div之一时,我希望它被克隆到其正上方的.view_full_img
div中。 I've tried using combinations of closest()
prev()
parent()
etc and just can't come up with something that works. 我试过使用closest()
prev()
parent()
等的组合,只是无法提出有用的东西。
Here's a jsfiddle... http://jsfiddle.net/dmcgrew/UeTv6/ 这是一个jsfiddle ... http://jsfiddle.net/dmcgrew/UeTv6/
You need parent()
, then prev()
: 您需要parent()
,然后是prev()
:
$(".thumbs div").click(function () {
$(this).parent().prev(".view_full_img").empty().append($(this).clone());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.