if find a div with a class then change the body class
Hi I am very new to jquery and stuck in a basic thing. what i want is:
If there is a div with class inner-header then apply the class link-bg on body if there is no inner-header div the remove the class. i am trying to do this with following code.
if ( $('body').find(.inner-header)开发者_StackOverflow中文版 ) {
$("body").addClass("link-bg");
} else {
$("body").removeClass("link-bg");
}
Try this:
$('body').toggleClass('link-bg', $('div.inner-header').length);
try this:
$(function(){
if($('.inner-header').length > 0){ $("body").addClass("link-bg"); }
else { $("body").removeClass("link-bg"); }
})
精彩评论