jQuery 删除只有 的元素 使用jQuery的空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1520981/
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
Remove elements with only a space using jQuery
提问by Dom
Is there a way to remove this
有没有办法去掉这个
<p> </p>
using jQuery?
使用jQuery?
回答by Roatin Marth
Try:
尝试:
$('p')
.filter(function() {
return $.trim($(this).text()) === '' && $(this).children().length == 0
})
.remove()
What that does is it finds all the <p>
s that have nothing in them, and removes them from the DOM.
它所做的是找到所有<p>
没有任何内容的s,并将它们从 DOM 中删除。
回答by Janson
As Greg mentions above, testing the trimmed .text() will remove paragraphs w/ no text, but do have a self-contained element like the <img>
tag. To avoid, trim the .html() return. As text is considered a child element in the DOM, you'll be set.
正如 Greg 上面提到的,测试修剪过的 .text() 将删除没有文本的段落,但确实有一个像<img>
标签这样的自包含元素。为了避免,修剪 .html() 返回。由于文本被视为 DOM 中的子元素,因此您将被设置。
$("p").filter( function() {
return $.trim($(this).html()) == '';
}).remove()
回答by orbitory
This could be a better solution for CMSs. Some rich text editors add
inside empty paragraphs.
这可能是 CMS 的更好解决方案。一些富文本编辑器
在空段落内添加。
$("p").filter( function() {
var html = $(this).html();
if(html == '' || html == ' ')
return true;
}).addClass('emptyP');
回答by MaLKaV_eS
回答by spatanx
$("p").filter( function() {
return $.trim($(this).html()) == '';
}).remove()
I used this to remove empty paragraph that contains not element such as IMG, Input, Select, etc.
我用它来删除不包含 IMG、输入、选择等元素的空段落。
回答by exoboy
If you are trying to simply remove all empty P elements, or P's with only a single space, then you could do this:
如果你想简单地删除所有空的 P 元素,或者只有一个空格的 P 元素,那么你可以这样做:
$('p').map( function(){
var html = $(this).html();
if( !html.length || html == ' ' || html == String.fromCharCode(255) )
return this;
}).remove();
This iterates through all of the P's on your page and if they match a certain criteria (they are empty or only have a whitespace) then it removes them from the DOM.
这将遍历页面上的所有 P,如果它们符合某个条件(它们是空的或只有一个空格),那么它会将它们从 DOM 中删除。
Also, by assigning our html content once to a local variable, it helps the script run faster. To look for non-breaking spaces I simply compare the contents to a string created from ASCII character code 255 (which is a non-breaking space).
此外,通过将我们的 html 内容一次分配给一个局部变量,它可以帮助脚本运行得更快。为了寻找不间断空格,我只需将内容与从 ASCII 字符代码 255(这是一个不间断空格)创建的字符串进行比较。
jQuery's map() function can be great when a simple filter, or attribute comparison will not suffice.
当简单的过滤器或属性比较不够用时,jQuery 的 map() 函数会很棒。
You can read more about it here.... http://api.jquery.com/map/
您可以在此处阅读有关它的更多信息.... http://api.jquery.com/map/
回答by lightyrs
This should handle any element with no children of any type(including unwrapped text nodes).
这应该处理没有任何类型的子元素(包括展开的文本节点)的任何元素。
$("p").filter(function() {
return !this.childNodes[0]
}).remove();
回答by Lance Rushing
give it an id (to get the selector).
给它一个 id(以获取选择器)。
<p id="myP"></p>
<script>
$("#myP").remove();
</script>