0

I have for eg #test on many page and in some page the #test div is not so I wanted to hide .myclass in which #test div is not. So I tried the following:

var $bs = $('body').find('#test');
    if($bs === 'undefined'){ // but this seems wrong also tried type of $bs === 'undefined'

        $('.myclass').css('display','none');
    }
4

3 回答 3

1

you could do:

if(!$bs.length){ 
    $('.myclass').css('display','none');
}
于 2013-09-13T10:55:23.717 回答
1
var $bs = $('#test');
if($bs.length == 0){
    $('.myclass').css('display','none');
}
于 2013-09-13T10:56:43.013 回答
0

rather than finding element, just check length using .length() function. It will return 0 if the element is not present and then you can write your code for that condition.

var $bs = $('#test').length();
if($bs == 0){ 
    $('.myclass').css('display','none');
}
于 2013-09-13T10:54:43.083 回答