How to add a class to empty h1 with jQuery?
I'd like to add a class 'hideme' to a empty div.
<div id="infocontent" class="grid_15 prefix_1">
<h1 id="mainhead" class="grid_15 alpha omega" ></h1>
<div id="infoinner" class="grid_13 prefix_1 suffix_1 alpha omega">
<FORM ACTION="command.asp" METHOD="get" NAME="artForm">
....
.....
I made this but something wrong.
$function(){
var contentid = $('#mainhead');
var content = contentid.html();
if 开发者_Go百科(content===''){
contentid.addClass('hideme');
}
})
Update: Sorry it was empty h1.
Your <div>
isn't empty; it contains all subsequent content until the end of <div id="infocontent">
.
Close your tags!
always!
As other people have mentioned, your code can be simplified by using the :empty
selector:
$('#mainhead:empty').addClass('hideme');
If the <div>
is not empty, this selector will not match any elements, and the line will do nothing.
You can try this:
$function(){
var contentid = $('#mainhead');
var content = contentid.html();
if (contentid.is(':empty')==''){
contentid.addClass('hideme');
}
})
Try this instead:
if (contentid.is(':empty')) {
contentid.addClass('hideme');
}
Try,
$("div:empty").addClass('hideme');
精彩评论