开发者

Detect a Variable Exists in jQuery

Hello I am rather new to jQuery and have a situation here.

I am actually setting default position on document.ready as below

$('.categorytitle').scrollTo($(".people .sants a.modeling").attr('href'), 750, {easing:'easeOutExpo'});

now this works perfectly when this is placed on the page containing .categorytitle class

The issue arises when the script cannot find .categorytitle class in the document

I tried 开发者_如何学Gothe below code but it did not work

if($('.categorytitle').length!= 0){
     $('.categorytitle').scrollTo($(".people .sants a.modeling").attr('href'), 750, {easing:'easeOutExpo'});
}

Can anyone help on a way I can find if .categorytitle exists in the page?

Many thanks!

This is the complete code I am trying to implement.

        if($('.categorytitle').length){
        $('.categorytitle').scrollTo($(".people a.modeling").attr('href'), 750, {easing:'easeOutExpo'});
        }
        if($('.categorytext').length){
        $('.categorytext').scrollTo($(".people a.modeling").attr('href'), 750, {easing:'easeOutExpo'});
        }
        if($('.models').length){
        $('.models').scrollTo($(".people a.modeling").attr('href'), 750, {easing:'easeOutExpo'});
        }

I get "f is undefined" is the jQuery ScrollTo Script file. Seems like the $(".people .sants a.modeling").attr('href') is undefined and so this error.


if($('.categorytitle')){
     $('.categorytitle').scrollTo($(".people .sants a.modeling").attr('href'), 750, {easing:'easeOutExpo'});
}

try this one


Your code works fine for me... what's the problem http://jsfiddle.net/mazlix/vAjDB/1/ and http://jsfiddle.net/mazlix/vAjDB/

this code works for me:

<div class="notcategorytitle">
    blahbalbhalh
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>
    if($('.categorytitle').length!= 0){
     alert('hit');
}
</script>


you should be able to just do

if ($('.categorytitle').length){ //your code here }


One way to ignore all possible errors in that line is to just catch and ignore exceptions in it:

try {
    $('.categorytitle').scrollTo($(".people .sants a.modeling").attr('href'), 750, {easing:'easeOutExpo'});
} catch(e) {}


Yes, Big thanks to all of you here - just a matter of seconds and you folks saved me hours of debugging - super cheers to you folks.

Below is the solution

        if($('.categorytitle').length && $(".people a.modeling").length){
            $('.categorytitle').scrollTo($(".people a.modeling").attr('href'), 750, {easing:'easeOutExpo'});
        }
        if($('.categorytext').length && $(".people a.modeling").length){
            $('.categorytext').scrollTo($(".people a.modeling").attr('href'), 750, {easing:'easeOutExpo'});
        }
        if($('.models').length && $(".people a.modeling").length){
            $('.models').scrollTo($(".people a.modeling").attr('href'), 750, {easing:'easeOutExpo'});
        }
    }

not only was the .categorytitle not being found as suggested by you folks, .people.a.modeling was also not found, so I checked for both.

Cheers to all of you here!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜