简体   繁体   English

链接未更改颜色JavaScript

[英]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: 是在做:

  • jQuery selector works from right to left. jQuery选择器从右到左起作用。 There are exceptions though, for example when the first operand is an ID. 但是,有例外,例如,当第一个操作数是ID时。 Then the search will operate in the context of the element with this ID. 然后,搜索将在具有此ID的元素的上下文中进行。
  • It will have all the a tags and look up for ancestor li and then elements with class .account_links. 它将具有所有a标记,并查找祖先li,然后查找具有.account_links类的元素。
  • It will target the clicked element. 它将定位到单击的元素。
  • It will remove all the active classes from anchor tag. 它将从定位标记中删除所有活动的类。
  • Adds the active class to the clicked element, ref by (this) context. 将活动类添加到单击的元素中,此上下文引用。

Your working fiddle : Jsfiddle 您的工作小提琴: Jsfiddle

Make sure you have included JQuery Library properly. 确保正确包含了JQuery库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM