开发者

if find a div with a class then change the body class

开发者 https://www.devze.com 2023-02-22 20:15 出处:网络
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 cl

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");  }

})
0

精彩评论

暂无评论...
验证码 换一张
取 消