jquery删除从另一个元素中删除 [英] jquery remove removing from another element
问题描述
根据此处,jquery的删除功能应该像这样工作..
According to here, jquery's remove function should work like so..
$('div').remove('selector');
我正在尝试。
Which I'm trying in this example.
HTML:
<div class="wrapper">
<p class="unwanted">This should be removed</p>
<p class="retained">This will remain</p>
</div>
JavaScript:
jQuery(document).ready(function($) {
$('div').remove('p.unwanted'); //not working
//$('p.unwanted').remove(); //this works
});
它不起作用。我做错了什么?
It's not working. What am I doing wrong?
推荐答案
你误解了文档的内容。它不是在寻找与匹配选择器匹配的匹配元素的后代的元素,它只是将已经匹配的元素集合过滤到与选择器匹配的元素,然后将它们删除。
You've misunderstood what the documentation is saying. It's not looking for elements that are descendants of the matched elements that match the selector, it's simply filtering down the set of already matched elements to those that match the selector, and then removing them.
如果你有这个HTML:
If you have this HTML:
<div class="wanted">Some text</div>
<div class="wanted">Some more text</div>
<div class="unwanted">Some unwanted text</div>
然后执行此jQuery:
and then executed this jQuery:
$('div').remove('.unwanted');
然后它只会删除第三个< div>
(上面带有不需要的
类的那个),因为它首先选择 all < div>
元素,然后只删除与选择器匹配的元素。
then it would only remove that third <div>
(the one with the unwanted
class on it), because it first selects all <div>
elements, and then only removes those that match the selector.
这篇关于jquery删除从另一个元素中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!