Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jquery强制删除元素?_Jquery - Fatal编程技术网

如何使用jquery强制删除元素?

如何使用jquery强制删除元素?,jquery,Jquery,我正在使用一个插件,我不知道从哪里重复相同的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&g

我正在使用一个插件,我不知道从哪里重复相同的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>

尝试改用父选择器

$('.test').parent('div').remove();
$('.test').clone().appendTo('#main');
最接近的方法将遍历祖先树,直到找到选择器匹配为止。它将返回0或1个元素

父方法只查看直接父对象。它将返回0个或多个元素

猜测插件在实例化时添加了你不想要的克隆,它似乎应该在某个插件事件上被克隆,我想这只是触发的?所以,在实例化你的插件并添加你不想要的克隆后再做

$('#main .test:not(:first)').parent().remove();
/* suppose this is in plugin */
$('.test').clone().appendTo('#main');
$('#main .test:not(:first)').parent().remove();

那么问题是什么呢??,你只是没有把jqueryjquery包含在fiddle@ArunPJohny中而已!对不起,我会更新。。。感谢您在dom ready Handler中添加代码请检查更新的演示…\n需要更多信息,最初问题的答案是因为您使用了错误的选择器,它只返回找到的第一个元素。您使用的插件是什么?如果对只包含一个元素的jQuery对象调用.closest和.parent,则它们最多返回一个元素。如果对包含许多元素的jQuery对象调用,这两种方法都可以返回许多元素。NavinRauniyar您的fiddle具有。下一个选择器,将其删除。看一看我在原始答案中发布的提琴。
$('#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();