admin管理员组文章数量:1317909
I'm trying my best to word this properly so please bear with me. So I'm trying to only toggle a class (active) on an element that is clicked. If another element is clicked I would like to add the class "active" to the element clicked and remove the active class from any other element.
The problem is when I click on one element the "active" class is added but then when I click on another element the "active" class isn't removed from the other elements
$("li.test a").click(function() {
$(this).toggleClass("active");
});
$("li.test a").click(function() {
$(this).toggleClass("active");
});
$("li.test a").click(function() {
$(this).toggleClass("active");
});
HTML
<li class="test1">
<a href="#" class="">
</li>
<li class="test1">
<a href="#" class="">
</li>
<li class="test1">
<a href="#" class="">
</li>
<li class="test2">
<a href="#" class="">
</li>
<li class="test2">
<a href="#" class="">
</li>
<li class="test2">
<a href="#" class="">
</li>
I'm trying my best to word this properly so please bear with me. So I'm trying to only toggle a class (active) on an element that is clicked. If another element is clicked I would like to add the class "active" to the element clicked and remove the active class from any other element.
The problem is when I click on one element the "active" class is added but then when I click on another element the "active" class isn't removed from the other elements
$("li.test a").click(function() {
$(this).toggleClass("active");
});
$("li.test a").click(function() {
$(this).toggleClass("active");
});
$("li.test a").click(function() {
$(this).toggleClass("active");
});
HTML
<li class="test1">
<a href="#" class="">
</li>
<li class="test1">
<a href="#" class="">
</li>
<li class="test1">
<a href="#" class="">
</li>
<li class="test2">
<a href="#" class="">
</li>
<li class="test2">
<a href="#" class="">
</li>
<li class="test2">
<a href="#" class="">
</li>
Share
Improve this question
asked Jul 11, 2016 at 17:21
SpankySpanky
7092 gold badges13 silver badges40 bronze badges
1
- Do you realize you are adding three click events to all the elements? – epascarello Commented Jul 11, 2016 at 17:31
5 Answers
Reset to default 5Here's the working code:
$("li.test a").on("click",function() {
if($(this).hasClass("active")){
$(this).removeClass("active");
} else{
$("a.active").removeClass("active");
$(this).addClass("active");
}
});
.active{
color: red;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<li class="test">
<a href="#" class="">test</a>
</li>
<li class="test">
<a href="#" class="">test</a>
</li>
<li class="test">
<a href="#" class="">test</a>
</li>
<li class="test">
<a href="#" class="">test</a>
</li>
<li class="test">
<a href="#" class="">test</a>
</li>
<li class="test">
<a href="#" class="">test</a>
</li>
(Working pen)
Be aware you have classes "test1" and "test2" in the HTML, but class "test" in the JS!
Use the not
method to skip your current element.
$("li.test a").click(function() {
var $this = $(this);
$('.test a').not($this).removeClass('active');
$this.toggleClass("active");
});
You have to remove the class from other elements before toggling.
Try this
$("li.test a").click(function() {
$("li.test a.active").removeClass("active");
$(this).toggleClass("active");
});
This first removes all the anchors with class active and then toggles the clicked anchor with the class.
Try this.
$("li").click(function(){
$(this).siblings().removeClass('active');
$(this).addClass('active');
})
li.active a{
color: red;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li class="test1 active">
<a href="#" class="">test</a>
</li>
<li class="test1">
<a href="#" class="">test</a>
</li>
<li class="test1">
<a href="#" class="">test</a>
</li>
<li class="test2">
<a href="#" class="">test</a>
</li>
<li class="test2">
<a href="#" class="">test</a>
</li>
<li class="test2">
<a href="#" class="">test</a>
</li>
</ul>
Go through below mentioned link or below mentioned code may be it can help you.
JSFiddle
HTML Code
<li class="test">
<a href="#" class="">test</a>
</li>
<li class="test">
<a href="#" class="">test</a>
</li>
<li class="test">
<a href="#" class="">test</a>
</li>
<li class="test">
<a href="#" class="">test</a>
</li>
<li class="test">
<a href="#" class="">test</a>
</li>
<li class="test">
<a href="#" class="">test</a>
</li>
CSS Code
.active{
color:#00adef;
}
JAVASCRIPT Code
$("a").click(function(){
$('a').removeClass('active');
$(this).addClass("active");
});
本文标签:
版权声明:本文标题:javascript - How do I toggle a class on click of an element but remove the same class from all other elements using JQuery? - St 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742027940a2415907.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论