2013-10-08 64 views
0

我正在使用一个插件,我不知道从哪里重复同一个div。所以我想删除重复的div如何用jquery强制删除元素?

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

<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'); 

updated demo

+1

问题是那么??,它的工作,你只是还没有包括jQuery的 – Rex

+2

jQuery是不包括在内的F iddle http://jsfiddle.net/arunpjohny/HeHks/1/ –

+0

@ArunPJohny哦!对不起,我会更新...谢谢 –

回答

-1

尝试使用父选择代替。

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

closest方法就遍历父类树,因为它需要去找到一个匹配选择。它将返回01元素。

parent方法只查看直接父项。它会返回0more元素

Fiddle

+0

我已经尝试过父母选择器太....但不成功.....删除.... –

+0

需要更多的信息,那么你最初的问题的答案是因为你使用了错误的选择器,只返回找到的第一个元素。你在使用什么插件? –

+0

[imgscroller for joomla 2.5](http://extensions.joomla.org/extensions/photos-a-images/rotators/images-rotators/8742) –

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();