使用 Jquery 在 div 后删除元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6497039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:57:36  来源:igfitidea点击:

Removing an element after a div with Jquery

jqueryjquery-selectorsdom-traversal

提问by JCHASE11

I would like to remove the p tag that directly follows a div using jquery. Here is my HTML:

我想使用 jquery 删除直接跟在 div 后面的 p 标记。这是我的 HTML:

<div class="fbcommentbox"></div>
<p>Powered by <a href="http://pleer.co.uk/wordpress/plugins/facebook-comments/">Facebook Comments</a></p>

So in this case, all content inside the <p>tags will be set to display:none.

所以在这种情况下,<p>标签内的所有内容都将被设置为display:none.

This seems like it would be VERY simple to do in jquery but I cannot seem to put my finger on it. Any help would be great. Thanks!

这似乎在 jquery 中很简单,但我似乎无法理解它。任何帮助都会很棒。谢谢!

回答by Ben Everard

This should work:

这应该有效:

$('.fbcommentbox').next('p').remove();

We select the div, then use nextto get the next element.

我们选择 div,然后使用它next来获取下一个元素。

回答by rid

$('div.fbcommentbox + p').hide();

Pick the one you need.

选择您需要的那个。

回答by AR.

$('.fbcommentbox').next().hide();or $('.fbcommentbox').css('display','none').

$('.fbcommentbox').next().hide();$('.fbcommentbox').css('display','none')