我有以下设置。我需要单击嵌套元素并在相对div之后克隆父元素。
例如:
<div class="container">
<div class="item class1">
<button class="nested"></button>
</div>
<div class="item class2">
<button class="nested"></button> //Click on this
</div>
<div class="item class3">
<button class="nested"></button>
</div>
<div class="item class4">
<button class="nested"></button>
</div>
</div>
这是我想要的结果:
<div class="container">
<div class="item class1">
<button class="nested"></button>
</div>
<div class="item class2">
<button class="nested"></button>
</div>
<div class="item class2"> // This is the resulting element at this specific position.
<button class="nested"></button>
</div>
<div class="item class3">
<button class="nested"></button>
</div>
<div class="item class4">
<button class="nested"></button>
</div>
</div>
答案 0 :(得分:2)
获取最接近的.item
,克隆它并将其自身归结为
$('.nested').on('click', function() {
var item = $(this).closest('.item'),
clone = item.clone();
item.after(clone);
});
请注意,要使其正常工作,您必须实际关闭元素。