gpt4 book ai didi

javascript - jquery remove 从另一个元素中删除

转载 作者:行者123 更新时间:2023-11-30 18:19:59 24 4
gpt4 key购买 nike

根据 here , jquery 的 remove 函数应该像这样工作..

$('div').remove('selector'); 

我正在尝试 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
});

它不工作。我做错了什么?

最佳答案

您误解了文档中的内容。它不是在寻找与选择器匹配的匹配元素的后代元素,它只是将已经匹配的元素集过滤到与选择器匹配的元素,然后删除它们。

如果你有这个 HTML:

<div class="wanted">Some text</div>
<div class="wanted">Some more text</div>
<div class="unwanted">Some unwanted text</div>

然后执行这个 jQuery:

$('div').remove('.unwanted');

然后它只会删除第三个 <div> (上面有 unwanted 类的那个),因为它首先选择 all <div>元素,然后仅删除与选择器匹配的元素。

Example jsFiddle

关于javascript - jquery remove 从另一个元素中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12296421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com