简体   繁体   English

如何使用 jquery 更改导航链接的颜色或背景颜色?

[英]How to change the color or background-color of the nav link using jquery?

Can someone know, what's wrong with my code?有人可以知道,我的代码有什么问题吗? It is when you are in the page the current color or background will be change.当您在页面中时,当前颜色或背景将发生变化。 example the color is of the nav is red, so when you go to the about us the color of it will be yellow and the rest of the link still red, Here is my fiddle:例如导航的颜色是红色的,所以当你去关于我们的时候它的颜色是黄色的,链接的其余部分仍然是红色的,这是我的小提琴:

https://jsfiddle.net/nhyr8pnd/ https://jsfiddle.net/nhyr8pnd/

 <ul class="topnav" id="main-menu">
      <li ><a href="#" class="active" ><i class="fa fa-home" aria-hidden="true"></i> Home</a></li>
      <li><a href="#" ><i class="fa fa-file" aria-hidden="true"></i> Home2</a></li>
      <li><a href="#" ><i class="fa fa-codepen" aria-hidden="true"></i> Home3</a></li>
      <li><a href="#" ><i class="fa fa-globe" aria-hidden="true"></i> Home4</a></li>
  </ul>


#main-menu li {
display: inline-block;
font-family: 'Raleway', sans-serif;

padding: 17px 25px;
}
#main-menu li a {
   color:#333333;
   font-size:15px;
}
#main-menu li.active a {
 color:#0198cf;
}
 #main-menu li:last-child  {
 padding-right: 0;
}
.active{background-color:#ccc;}



$(document).ready(function(e){
  $('#main-menu li').click(function(e) {
    $('#main-menu li').removeClass('active');
    $(this).addClass('active');
  });
});

Ps: I'm using header.php, so I don't need to copy all the nav in every page, so basically if the answer is putting an active class in every page is not an answer Ps:我正在使用header.php,所以我不需要复制每个页面中的所有导航,所以基本上如果答案是在每个页面中放置一个活动类不是答案

You are selecting li instead you should select a inside of li on click event.您正在选择 li 而不是您应该在单击事件时选择 li 的内部。

$(document).ready(function(e){
    $('#main-menu li').click(function(e) {
      $('#main-menu li a').removeClass('active');
      $(this).children("a").addClass('active');
    });
  });

Please check .It's work.请检查。它的工作。 You forget to put jQuery min file.您忘记放置 jQuery 最小文件。

 $(document).ready(function(e){ $('#main-menu li a').click(function(e) { $('#main-menu li a').removeClass('active'); $(this).addClass('active'); }); });
 #main-menu li { display: inline-block; font-family: 'Raleway', sans-serif; padding: 17px 25px; } #main-menu li a { color:#333333; font-size:15px; } #main-menu li.active a { color:#0198cf; } #main-menu li:last-child { padding-right: 0; } .active{ background-color:#ccc; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="topnav" id="main-menu"> <li ><a href="#" class="active" ><i class="fa fa-home" aria-hidden="true"></i> Home</a></li> <li><a href="#" ><i class="fa fa-file" aria-hidden="true"></i> Home2</a></li> <li><a href="#" ><i class="fa fa-codepen" aria-hidden="true"></i> Home3</a></li> <li><a href="#" ><i class="fa fa-globe" aria-hidden="true"></i> Home4</a></li> </ul>

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

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