0

我正在使用一个插件,但我不知道从哪里重复相同的 div。所以我试图删除重复的 div

假设我有以下标记......

<div id="main">
<div>
    <div>
<div>
    <p class="test">hi this is test</p>
</div>
<div>
    <p class="test">hi this is test</p>
</div>
    </div>
</div>
    </div>

jQuery

/*how can I forcefully remove this so that later on this div cannot be cloned*/
$('.test').closest('div').next().remove();
/* suppose this is in plugin */
$('.test').clone().appendTo('#main');

更新的演示

4

2 回答 2

0
$('#main .test:not(:first)').parent().remove();

猜测插件在实例化时添加了你不想要的克隆,它似乎(应该在一些插件事件触发时克隆我认为?)所以在实例化你的插件后添加你不想要的克隆

$('#main .test:not(:first)').parent().remove();
/* suppose this is in plugin */
$('.test').clone().appendTo('#main');
$('#main .test:not(:first)').parent().remove();
于 2013-10-08T05:41:29.240 回答
-1

尝试改用父选择器。

$('.test').parent('div').remove();
$('.test').clone().appendTo('#main');

closest方法向上遍历祖先树,直到找到一个选择器匹配。它将返回一个01元素。

parent方法仅查看直接父级。它将返回0more元素

小提琴

于 2013-10-08T05:17:22.620 回答