开发者

How to print value of .hasClass via console?

using the Firebug console I'm trying to test whether this code is working:

$(window).bind("load", function() {
    $('#tab1link').click(function() {
        $('#tab2').hide();
        $('#tab2').removeClass('selected');
        $('#tab1').show();
        $('#tab1').addClass('selected');
    });

    $('#tab2link').click(function() {
        $('#tab1').hide();
        $('#tab1').removeClass('selected');
        $('#tab2').show();
        $('#tab2').addClass('selected');
    });

});

but this:

console.log($('#tab2').hasClass('selected'))

returns the error:

 Ty开发者_StackOverflow中文版peError: $("#tab2").hasClass is not a function { message="$("#tab2").hasClass is not a function",  more...}

Does anyone know why the above console command is incorrect? (Not a jQuery expert...)

Based on the link below, I think it should work... http://api.jquery.com/hasClass/

Thanks!


Try refreshing the console/page because sometimes it doesn't properly assign $ to jQuery, assuming you're using Firebug ( it keeps the native $ function which ISNT jquery ).

You can confirm this with:

alert( $ == jQuery )

If this isn't it, then make sure you aren't using multiple libraries that use $.

Unrelated: You can also do $(function(){ /* code */ }); instead of binding on window load.


Try this instead:

console.log(jQuery('#tab2').hasClass('selected'))

It looks suspiciously like the $ is given up but still in use, like Prototype in included in the page and jQuery.noConflict() was called (which means $ != jQuery when you're running commands in the console later).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜