简体   繁体   中英

jQuery addClass to 'THIS' element

I tried to addclass to element on click. used addClass() but no luck

jQuery(".act_nav").on("click",function(){
    var str=jQuery(this).attr('id');
    var title=jQuery(this).attr('title');
    jQuery(this).addClass('active');
});

HTML

 <li  id="act_about-us" class="act_nav" title="About US"><span id="ic_abt"></span><a href="about-us">About</a></li>

CSS

 .active  #ic_abt{ background-position: -37px  -448px;  }

Your problem is in your css.

.active #ic_abt{ background-position: -37px -448px; }

Only works if the class is active and the ID is ic_abt . But your ID is act_about-us .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM