[英]How do I add then remove class on outer div on each clink
If id
of li a
item matches the class of div
item a want to add a class
"theme" on each click. 如果
li a
id
项与div
项的类别匹配,则希望在每次点击时添加一个class
“ theme”。
If id
of li a
item doesn't matches the class of div
item a want to remove the class "theme" from non matching divs. 如果
li a
id
项与div
项的类不匹配,则要从不匹配的div中删除“主题”类。
But it only adds the class on each click but doesn't remove class. 但是,它只会在每次点击时添加课程,而不会删除课程。 Any suggestions?
有什么建议么?
<ul class="funding-theme">
<li data-rel="all"><a class="sub active" id="all" href="javascript:void(0)">All</a></li>
<li><a class="sub" id="education" href="javascript:void(0)">Education</a></li>
<li><a class="sub" id="healthcare" href="javascript:void(0)">Healthcare</a></li>
<li><a class="sub" id="justice" href="javascript:void(0)">Justice</a></li>
</ul>
<div class="list left">
<div class="non_profit all education"></div>
<div class="non_profit all education"></div>
<div class="non_profit all justice"></div>
<div class="non_profit all healthcare"></div>
</div>
<script>
var $sub = $(".sub");
$sub.click(function(e){
if (e.target.id == "all") {
//alert("#" + $(this).attr('id') + "");
$("."+$(this).attr('id')).removeClass('theme');
} else if (e.target.id == $(this).attr('id')) {
alert("#" + $(this).attr('id') + "");
$("."+$(this).attr('id')).removeClass('theme');
$("."+$(this).attr('id')).addClass('theme');
}
});
</script>
Try this 尝试这个
$('.sub').click(function (e) {
var id = ($(this).attr("id"));
$('.list').find('div').each(function(){
if($(this).hasClass(id))
{ $(this).addClass('theme');
}else
{
$(this).removeClass('theme');
}
});
});
Try this: 尝试这个:
var $sub = $(".sub");
$sub.click(function(e){
if (e.target.id == "all") {
//alert("#" + $(this).attr('id') + "");
$(".theme").removeClass('theme');
} else if (e.target.id == $(this).attr('id')) {
alert("#" + $(this).attr('id') + "");
$(".theme").removeClass('theme');
$("."+$(this).attr('id')).addClass('theme');
}});
If I got this right, you need to change the following lines: 如果我做对了,则需要更改以下几行:
$("."+$(this).attr('id')).removeClass('theme');
$("."+$(this).attr('id')).addClass('theme');
to 至
$(".all").removeClass('theme');
$("."+$(this).attr('id')).addClass('theme');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.