2013-10-08 63 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();