给出一类“选定的"类别.当点击另一个< a>时如果同一个< a> ;,则删除该类. [英] Give a class of "selected" when clicked on another <a> then remove the class if it's the same <a>
本文介绍了给出一类“选定的"类别.当点击另一个< a>时如果同一个< a> ;,则删除该类.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试更改
$('h2 a').on('click', function(){
$('h2 a.selected').removeClass('selected');
$(this).addClass('selected');
});
我做了一个手风琴,第一个选项卡的类为selected
.因此,当我单击另一个选项卡时,它会从第一个选项卡中删除selected
类,并提供我单击的selected
类,如果我单击刚打开的同一选项卡,它将删除selected
.
I made an Accordion, the first tab has class selected
. So when I click on another tab, it removes selected
class from the first tab and gives the one I clicked on selected
, if I click on the same tab I just opened, it removes selected
.
推荐答案
您可以使用.is()
,.filter()
var h2 = $("h2 a");
h2.on("click", function() {
if ($(this).is(".selected")) {
$(this).removeClass("selected");
} else {
h2.removeClass("selected")
.filter(this).addClass("selected")
}
});
.selected {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<h2>
<a>a</a>
<a>b</a>
<a>c</a>
</h2>
这篇关于给出一类“选定的"类别.当点击另一个< a>时如果同一个< a> ;,则删除该类.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文