ignore empty <p> </p> tags in jquery
I have jquery code that puts borders around <p>
tags but it is also doing so for <p>
tags with no text or child nodes in them. I was wondering if I could ignore <p>
tags that have no content inside them. Would "not开发者_JS百科 has" be considered here?
p tags with spaces should also be ignored
Try:
$("p:not(:empty)").css("border", "1px solid red");
You can try it here.
Try this code:
$('p:not(:empty)')
You can use the :empty
selector to find elements with no children (including text nodes).
$('p:not(:empty)')
Note that any text content -- even a single white space -- will be selected by this.
If you want to ignore whitespace as well you can do
$(document).ready(function() {
$("p").filter(function(){
return $.trim($(this).text()) !== '';
}).addClass("class-with-border");
});
demo at http://jsfiddle.net/gaby/nnVCF/1/
You need the :empty
selector. Like this:
$("p:not(:empty)").css("border", "1px solid #F00");
Try this
$("p:not(:empty)");//It will select all the tags which hav children
Check out the :not() and :empty selectors
$('p:not(:empty)')
try this
$("p").each(function(index,value){
if($(value).html()!="")$(value).css({border:"1px solid black"});
});
You can try with the :empty
selector
$('p:empty').remove();
精彩评论