简体   繁体   English

jQuery addClass 到“这个”元素

[英]jQuery addClass to 'THIS' element

I tried to addclass to element on click.我试图在单击时将类添加到元素。 used addClass() but no luck使用 addClass() 但没有运气

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

HTML 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 CSS

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

Your problem is in your css.你的问题出在你的css中。

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

Only works if the class is active and the ID is ic_abt .仅当类active且 ID 为ic_abt But your ID is act_about-us .但是您的 ID 是act_about-us

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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