开发者

Why is alert($('.myClass').hasClass('myClass')); false?

开发者 https://www.devze.com 2023-01-20 19:47 出处:网络
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

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'));
0

精彩评论

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