Why is alert($('.myClass').hasClass('myClass')); false?
When the page loads i replace the attributes of a specific object. Then in a few lines under(maybe directly under) i do another check which always seems to fail. So i tried
alert($('.myClass').hasClass('myClass'));
and by all logic shouldnt that always return true? I set a breakpoint in firebug and i can see the object with the new attribute however... why does t开发者_JAVA百科his line fail?
by all logic shouldnt that always return true?
It will return false
if your selector matches no elements. Are you sure that's not the case? You can try this to check:
alert($('.myClass').length);
How about this?
var myclasses = $('.myClass');
alert(!myclasses.length || myclasses.hasClass('myClass'));
精彩评论