简体   繁体   English

活动课程不起作用

[英]Active class not working

My nav menu with index is given "active" class. 我的带有索引的导航菜单被赋予“活动”类。 What I want is to set "active" when I click other <li> elements. 我想要的是单击其他<li>元素时将其设置为“活动”。 But it switches back to index.php . 但是它将切换回index.php What I mean is: it's not setting "active" class to other <li> nor removing from "home". 我的意思是:这不是将“活动”类设置为其他<li>也不是将其从“主页”中删除。

Below is what I have tried using jQuery, but its not working. 以下是我尝试使用jQuery进行的尝试,但无法正常工作。
What's going wrong? 怎么了

 $('.nav').on('click', 'li', function() { $('.nav li.active').removeClass('active'); $(this).addClass('active'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav navbar-right"> <li class="dropdown active"> <a href="index.php">Home</a> </li> <li class="dropdown"> <a href="aboutus.php">About</a> </li> <li class="dropdown"> <a href="services.php">Services</a> </li> <li class="dropdown"> <a href="contact.php">Contact</a> </li> </ul> </div> 

When you click the link in the li, it takes you to the page in the href attribute and loads it (or reloads the current page), wiping out any changes you've made with javascript. 当您单击li中的链接时,它会将您带到href属性中的页面并进行加载(或重新加载当前页面),并清除您对javascript所做的任何更改。

I think javascript might not be exactly what you're looking for here. 我认为javascript可能与您在这里找不到的完全一样。 I see you're using php, so I would recommend using that to keep track of which page the user is on instead. 我看到您使用的是php,因此建议您使用它来跟踪用户所在的页面。 With php, if this list is included on all pages you'd want to do something like this at the top of each page: 使用php,如果此列表包含在所有页面上,则您希望在每个页面的顶部执行以下操作:

<?php $pageName = "index"; ?> // aboutus, services etc.

Then down in your li's add something like this: 然后在您的li中添加以下内容:

<li class="dropdown <?php if (pageName === "index") echo "active"; ?>">
    <a href="index.php">Home</a>
</li>
<li class="dropdown  <?php if (pageName === "aboutus") echo "active"; ?>">
    <a href="aboutus.php">About</a>
</li>

If you were recreating this list manually on each page (not recommended), you'd rewrite each page like this: 如果要在每个页面上手动重新创建此列表(不建议使用),则可以这样重写每个页面:

Home Page 主页

<li class="dropdown active">
    <a href="index.php">Home</a>
</li>
<li class="dropdown">
    <a href="aboutus.php">About</a>
</li>

About Us page 关于我们页面

<li class="dropdown">
    <a href="index.php">Home</a>
</li>
<li class="dropdown active">
    <a href="aboutus.php">About</a>
</li>

etc. 等等

Your code is setting and removing the active class as it should. 您的代码正在设置和删除active类。 The problem is that as soon as you click a link, you navigate away from the page. 问题是,一旦单击链接,便会离开页面。

If your goal is to "remember" what was the last clicked link when you return to the page you need to store that in a persistent medium. 如果您的目标是“记住”返回页面时最后单击的链接,则需要将其存储在持久性介质中。 localStorage is simple to use and would do the trick. localStorage使用简单,可以解决问题。

But, you have your click event set up on your li elements and those li elements contain a elements, which is redundant. 但是,您在li元素上设置了click事件,并且这些li元素包含a元素,这是多余的。 Instead, just set the click on the a elements. 相反,只需在a元素上click

 $(function(){ // When the document is ready, get the last active link (if any) var lastActive = localStorage.getItem("lastClicked"); // Set that element as the active element $("a[href='" + lastActive + "']").addClass("active"); $('.nav').on('click', 'a', function(e){ $('.nav li.active').removeClass('active'); $(this).addClass('active'); // Set the clicked link's href value into localStorage localStorage.setItem("lastClicked", this.getAttribute("href")); }); }); 
 .active { background:#ff0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav navbar-right"> <li class="dropdown active"> <a href="index.php">Home</a> </li> <li class="dropdown"> <a href="aboutus.php">About</a> </li> <li class="dropdown"> <a href="services.php">Services</a> </li> <li class="dropdown"> <a href="contact.php">Contact</a> </li> </ul> </div> 

Keep in mind that this code will not work, here in the Stack Overflow code snippet environment due to security restrictions, but it will work in your environment. 请记住,由于安全性限制,此代码在堆栈溢出代码段环境中不起作用,但在您的环境中将起作用。

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

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