[英]Links not changing colour JavaScript
I have searched the internet for help on creating links that work like radio buttons as shown here: 我在互联网上搜索了有关创建类似于单选按钮的链接的帮助,如下所示:
http://jsfiddle.net/CXrgm/6/ http://jsfiddle.net/CXrgm/6/
However after trying many different attempts, I just don't get why it doesn't work. 但是,在尝试了许多不同的尝试之后,我只是不明白为什么它不起作用。 All that happens is that my active link stays active and none of the other links change to active class.
发生的一切是我的活动链接保持活动状态,而其他任何链接都没有更改为活动类。
$('.account_links li a').click(function() { $('.account_links li a').removeClass('active'); $(this).addClass('active'); });
.account_links { list-style-type: none; margin-top: 20px; margin-left: 30px; margin-right: 50px; font-family: 'Raleway', sans-serif; font-size: 20px; display: block; } .account_links a { color: #08c; text-decoration: none; } .account_links li { padding-bottom: 30px; } .account_links a:hover { text-decoration: underline; } .account_links .active { color: #08c08c; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class='account_links'> <li><a href='#' class="active">Home</a></li> <li><a href='#'>Shop</a></li> <li><a href='#'>About</a></li> <li><a href='#'>Contact</a></li> </ul>
I know I could set each link to active for each webpage, but I am doing something with JavaScript 'onclicks' and I don't think it will work but what the jsfiddle shows is exactly what I need. 我知道我可以将每个网页的每个链接设置为活动状态,但是我正在使用JavaScript“ onclicks”进行操作,但我认为它不会起作用,但是jsfiddle显示的正是我所需要的。
If your trying to implement the li's dynamically use .on for binding its events. 如果您尝试动态实现li,请使用.on绑定其事件。 eg.
例如。
$("selector").on("event", function(){})
. $("selector").on("event", function(){})
。 This makes sure that the events will be binded to the element once it is created in DOM. 这样可以确保事件在DOM中创建后将被绑定到该元素。 However your scenario seems to static html ul li's.
但是,您的情况似乎是静态html ul li的。 In simple words here is what your code
$('.account_links li a').click(function() { $('.account_links li a').removeClass('active'); $(this).addClass('active'); });
简单来说,这就是您的代码
$('.account_links li a').click(function() { $('.account_links li a').removeClass('active'); $(this).addClass('active'); });
is doing: 是在做:
Your working fiddle : Jsfiddle 您的工作小提琴: Jsfiddle
Make sure you have included JQuery Library properly. 确保正确包含了JQuery库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.