Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 给一个“类”;精选;单击另一个<;a>;然后删除该类,如果它是';这是一样的<;a>;_Javascript_Jquery - Fatal编程技术网

Javascript 给一个“类”;精选;单击另一个<;a>;然后删除该类,如果它是';这是一样的<;a>;

Javascript 给一个“类”;精选;单击另一个<;a>;然后删除该类,如果它是';这是一样的<;a>;,javascript,jquery,Javascript,Jquery,我正在努力改变 $('h2 a').on('click', function(){ $('h2 a.selected').removeClass('selected'); $(this).addClass('selected'); }); 我做了一个手风琴,第一个选项卡选择了class。因此,当我单击另一个选项卡时,它会从第一个选项卡中删除选中的类,并给出我单击的选中的,如果我单击刚才打开的同一个选项卡,它会删除选中的您可以使用.is(),.filter() var h2=$

我正在努力改变

$('h2 a').on('click', function(){
    $('h2 a.selected').removeClass('selected');
    $(this).addClass('selected');
});
我做了一个手风琴,第一个选项卡选择了class
。因此,当我单击另一个选项卡时,它会从第一个选项卡中删除
选中的
类,并给出我单击的
选中的
,如果我单击刚才打开的同一个选项卡,它会删除
选中的

您可以使用
.is()
.filter()

var h2=$(“h2 a”);
h2.打开(“单击”,函数(){
如果($(this).is(“.selected”)){
$(此).removeClass(“选定”);
}否则{
h2.removeClass(“选定”)
.filter(此).addClass(“选定”)
}
});
。已选定{
颜色:红色;
}

A.
B
C

请检查下面的答案,在h2标签中添加一个类,这样它就不能用于其他h2标签了。单击任意一个时,只需删除“选定的”类,并在当前元素中添加“选定的”类

Html:


Jquery:

<script type="text/javascript">
    jQuery('.headerTitle a').on('click', function(){
        jQuery('.headerTitle a').removeClass('selected');
        jQuery(this).addClass('selected');
    });
</script>

jQuery('.headertile a')。在('click',function()上{
jQuery('.headerTitle a').removeClass('selected');
jQuery(this.addClass('selected');
});
CSS:


.选定{颜色:红色;}

我想是一样的,因为这个标记对象在内存中没有改变。只是标记属性改变了。你想做什么?非常感谢。工作得很有魅力。
<script type="text/javascript">
    jQuery('.headerTitle a').on('click', function(){
        jQuery('.headerTitle a').removeClass('selected');
        jQuery(this).addClass('selected');
    });
</script>
<style type="text/css">
    .selected{ color: red; }
</style>