I have some div\'s that maybe be empty (depending on server-side logic).
-
$('div').each(function() {
if($(this).html().size() == 0) $(this).remove();
});
If you want to use the divs later on in the same page, it's better to use $(this).hide();
instead of $(this).remove();
as the divs will be deleted if you use remove();
讨论(0)
-
Here is a CSS3 solution for anyone who is interested
div:empty {
display:none;
}
讨论(0)
-
jQuery has a :empty selector. So, you can simply do:
$('div.section:empty').hide();
讨论(0)
-
replace display:block;
by display: none;
edit: Oh, i saw you wanted to use jQuery, then use .hide(): http://api.jquery.com/hide/
讨论(0)
-
If div contains white-space or line breaks then this code may be helpful...
$(document).ready(function() {
str = $('div.section').text();
if($.trim(str) === "") {
$('div.section').hide();
}
});
讨论(0)
-
Why does nobody use .filter ?
$("div.section").filter(function() {
return this.childNodes.length === 0;
}).hide();
This is a more elegant solution compared to using .each
.
讨论(0)