jQuery 从特定 div 中删除 <br>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11237838/
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 10:22:42  来源:igfitidea点击:

remove <br> from specific div

jquery

提问by user1450749

I need to remove break from a specific div, i know how to remove all breaks with

我需要删除特定的中断div,我知道如何删除所有中断

$('br').remove();?

$('br').remove();?

But I need to keep some breaks elsewhere on the page ,

但我需要在页面的其他地方保留一些休息时间,

How would I remove the breaks from a divwith the id "grape1" and "grape12" but leave all others intact?

如何从divid 为“grape1”和“grape12”的a 中删除中断,但保留所有其他中断?

回答by xdazz

$('#grape1,#grape12').find('br').remove();

回答by Brombomb

$('br', '#grape1, #grape12').remove();?

The second argument in the $selector allows you to filter your search to a given subset, instead of the entire DOM. In this case we tell jQuery to only look inside the grape1and grape12divs. This will match all br's in those div's.

$选择器中的第二个参数允许您将搜索过滤到给定的子集,而不是整个 DOM。在这种情况下,我们告诉 jQuery 只查看grape1grape12div内部。这将匹配那些 div 中的所有 br。

JsFiddle

提琴手

回答by Sgaddo

If that doesn't work (as happened in my case also) it's probably because the <br>are generated by some scripts after the JS .remove()code execution.

如果这不起作用(在我的情况下也发生过),可能是因为<br>JS.remove()代码执行后由某些脚本生成。

To check if I was right I set a quite long timeout and all my unwanted <br>disappeared after that time. So, in the end I just decided to put a simple CSS

为了检查我是否正确,我设置了一个很长的超时时间,<br>在那之后我所有不需要的都消失了。所以,最后我决定放一个简单的 CSS

#grape1 br, #grape12 br { display: none; }

#grape1 br, #grape12 br { display: none; }

and eveybody's happy =)

并且每个人都很开心 =)