简体   繁体   English

更改活动链接颜色

[英]Change active link color

Problem问题

I have an issue with my website, I want to change the color of the selected link to blue, and if other is selected put it back to gray, but don`t know how to target the clicked link in javascript.我的网站有问题,我想将所选链接的颜色更改为蓝色,如果选择了其他链接,则将其恢复为灰色,但不知道如何在 javascript 中定位点击的链接 Here is my code.这是我的代码。

 var center = document.getElementsByClassName("center"); for (let i = 0; i < center.length; i++) { center[i].addEventListener("click", DivSelector()); } function DivSelector() { for (let i = 0; i < center.length; i++) { center[i].classList.Remove('active'); }; }
 .nav-item { text-decoration: none; color: #505b67; margin-left: 10px; } .active { color: #4460f1; }
 <ul class="nav-tab-ul"> <li id="Profile"> <a class="center nav-item" href="">Profile</a> </li> <li id="Change-Password"> <a class="center nav-item" href="">Change password</a> </li> <li id="Notifications"> <a class="center nav-item" href="">Notifications</a> </li> <li id="My-Cards"> <a class="center nav-item" href="">My Cards</a> </li> </ul>

Don`t know how to select the clicked link here for adding the 'active' class.不知道如何在此处选择单击的链接以添加“活动”类。

All you need to do is add the class active to the actual link of the page you are on.您需要做的就是将活动类添加到您所在页面的实际链接中。 So when you on the profile page you add active to profile link and none of the others因此,当您在个人资料页面上时,您将活动添加到个人资料链接中,而其他人都没有

PROFILE PAGE个人资料页

<ul class="nav-tab-ul">
            <li id="Profile">
              <a class="center nav-item active" href="">Profile</a>
            </li>
            <li id="Change-Password">
              <a class="center nav-item" href="">Change password</a>
            </li>
            <li id="Notifications">
              <a class="center nav-item" href="">Notifications</a>
            </li>
            <li id="My-Cards">
              <a class="center nav-item" href="">My Cards</a>
            </li>
  </ul>

CHANGE PASSWORD PAGE更改密码页面

When you on the change password page you add the active class to that link and none of the others.当您在更改密码页面上时,您将活动类添加到该链接,而其他任何类都没有。

<ul class="nav-tab-ul">
                <li id="Profile">
                  <a class="center nav-item" href="">Profile</a>
                </li>
                <li id="Change-Password">
                  <a class="center nav-item active" href="">Change password</a>
                </li>
                <li id="Notifications">
                  <a class="center nav-item" href="">Notifications</a>
                </li>
                <li id="My-Cards">
                  <a class="center nav-item" href="">My Cards</a>
                </li>
</ul>

If these are separate pages there is no need for JS?如果这些是单独的页面,就不需要 JS 吗?

If you are using one header file and including the file into all your pages then it will be a different solution.如果您使用一个头文件并将该文件包含在所有页面中,那么这将是一种不同的解决方案。 Let us know why you want to use JS让我们知道您为什么要使用 JS

A few things:一些东西:

  1. When adding your event listener, you called DivSelector, meaning you added the return value of DivSelector() as a listener, which is void.添加事件侦听器时,调用了 DivSelector,这意味着将 DivSelector() 的返回值添加为侦听器,该值是无效的。 Instead just pass the name of the function you want to add as a listener.而只是传递您要添加为侦听器的函数的名称。

  2. You need to add the 'active' class to the clicked element after removing it from all the others.从所有其他元素中删除后,您需要将“活动”类添加到单击的元素中。 You can get the clicked element by using the event argument passed to your event listener.您可以使用传递给事件侦听器的event参数来获取单击的元素。 The element will be event.target .该元素将是event.target

  3. DivSelector should be defined before it's used, so it should be moved above the rest of the code. DivSelector 应在使用之前定义,因此应将其移至其余代码的上方。

Here's an example:下面是一个例子:

 function DivSelector(event) { for (let i = 0; i < center.length; i++) { center[i].classList.remove('active'); } event.target.classList.add('active'); } let center = document.querySelectorAll("center"); for (let i = 0; i < center.length; i++) { center[i].addEventListener("click", DivSelector); }
 .nav-item { text-decoration: none; color: #505b67; margin-left: 10px; } .active { color: #4460f1; }
 <ul class="nav-tab-ul"> <li id="Profile"> <a class="center nav-item" href="#">Profile</a> </li> <li id="Change-Password"> <a class="center nav-item" href="#">Change password</a> </li> <li id="Notifications"> <a class="center nav-item" href="#">Notifications</a> </li> <li id="My-Cards"> <a class="center nav-item" href="#">My Cards</a> </li> </ul>

You can get the element clicked in event.currentTarget .您可以在event.currentTarget单击元素。

 const nodes = document.getElementsByClassName("center"); for (let i = 0; i < nodes.length; i++) { nodes[i].addEventListener("click", divSelector); } function divSelector(event) { removeAllActives(); event.currentTarget.className += ' active'; } function removeAllActives() { const actives = document.getElementsByClassName("active"); for (let i = 0; i < actives.length; i++) { actives[i].classList.remove('active'); } }
 .active { color: blue; }
 <a class="center"> Link 1 </a> <br/> <a class="center"> Link 2 </a>

Try this.尝试这个。 its working它的工作

  var center = document.getElementsByClassName("center");
  for (let i = 0; i < center.length; i++) {
    center[i].addEventListener("click", function(e){
      for (let i = 0; i < center.length; i++) {
         center[i].classList.remove('active');
         e.target.classList.add('active');
      }
    })
  }

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

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