删除“空" jQuery的段落 [英] Remove "empty" paragraphs with jQuery
本文介绍了删除“空" jQuery的段落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的作家有创建空段落的坏习惯. (即,其中没有任何内容的段落,例如:
My writers have a bad habit of creating empty paragraphs. (I.e., paragraphs that have nothing inside them, like:
<p></p>
有没有删除空"段的jQuery函数.这是我尝试过的:
Is there a jQuery function that removes "empty" paragraphs. Here's what I've tried:
$('p').remove(":contains(' ')"); // nope! bad logic, all p's have empty spaces
$("p:empty").remove() // nope, doesn't work, doesn't remove any p's
还有什么我应该尝试的吗?
Anything else I should try?
推荐答案
$("p:empty").remove()
works for me (demo at http://jsfiddle.net/AndyE/XwABG/1/). Make sure your elements are actually empty and don't contain any white space.
您始终可以使用 filter()
:
You can always use filter()
:
// Trimming white space
$('p').filter(function () { return $.trim(this.innerHTML) == "" }).remove();
// Without trimming white space
$('p').filter(function () { return this.innerHTML == "" }).remove();
正在运行的演示: http://jsfiddle.net/AndyE/XwABG/
这篇关于删除“空" jQuery的段落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文