在Anchor标记的点击添加类 [英] adding a class on click of Anchor tag

查看:148
本文介绍了在Anchor标记的点击添加类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下HTML ...

Lets say I have the following HTML...

<a class="active" href="#section1">Link 1</a>  
<a href="#section2">Link 2</a>  

点击链接2时,我想接收活动类并从链接中删除类1本身,因此它将有效地变为:

When a link 2 is clicked I would like it to receive the active class and remove the class from link 1 itself so it would effectively become:

<a href="#section1">Link 1</a>  
<a class="active" href="#section2">Link 2</a>

这应该可以工作。也就是说。无论链接是否被点击获取类并将其从其他对象中删除。

This should work both ways. Ie. whatever link is clicked gets the class and removes it from the other.

如何使用jQuery做到这一点?
提前感谢!

How can this be done with jQuery? Thanks in advance!

推荐答案

$("a").click(function(){
   $("a.active").removeClass("active");
   $(this).addClass("active");
});

编辑:添加jsfiddle

added jsfiddle

http://jsfiddle.net/LJ6L5/

这篇关于在Anchor标记的点击添加类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆