当前位置: 动力学知识库 > 问答 > 编程问答 >

after adding dynamic class how to check the class is existing using jquery

问题描述:

here is my Question,

I have added a class to a 'div' using jquery like this,

$('.myClass').addClass('active')

after adding this class I want to check whether 'active' is existing or not

for that I have written like this

if( $('.myClass').hasClass('active')) {

alert('active');

}

But this is not working somebody please help me. And help will be really appreciated.

thank you.

网友答案:

You've missed out a capital letter in your selector, .myclass should be .myClass:

if( $('.myClass').hasClass('active')) {
    alert('active');
}
网友答案:

The problem is in the selector (jquery is case sensitive) change myclass to myClass like this:

$('.myClass').addClass('active');
if( $('.myClass').hasClass('active')) {
    alert('active');
}

DEMO

网友答案:

class Names are case sensitive .You used myClass instead of myclass

if( $('.myClass').hasClass('active')) {
        alert('active');
    }

Case Sensitivity in class and id Names

分享给朋友:
您可能感兴趣的文章:
随机阅读: