简体   繁体   English

活动链接不显示刷新页面时添加的类名称,也不显示从其他页面单击相同链接时添加的类名称

[英]Active links don't show the class name added when the page is refreshed or when the same link is clicked from another page

Script: 脚本:

$(".classname li a").each(function() {
    var hreflink = $(this).attr("href");
    if (hreflink == location.href) {
        $(this).addClass("active");
    } else {
        $(this).removeClass("active");
    }
});

CSS: CSS:

.classname li a.active{color:#ef9223;}

Whenever I do a page refresh, if the same link is on a different page, the active class is no longer applied to that link. 每当我进行页面刷新时,如果相同的链接在不同的页面上,则活动类不再应用于该链接。

Actual exmple i have the following Navigation links ABCDEFGH .... Z. And the same Navigation is in the main page(header section).. so when i click on any one link .. it needs to be active on when i arrive to any one ABCD E..or Z pages. 实际的例子我有以下导航链接ABCDEFGH .... Z.相同的导航是在主页面(标题部分)..所以当我点击任何一个链接..当我到达时它需要激活任何一个ABCD E..or Z页面。 And even on page refresh it needs to retain the active link. 即使在页面刷新时,它也需要保留活动链接。 Hope that explains...and helps :) 希望解释......并帮助:)

Any help, inputs, solution would be much appreciated. 任何帮助,输入,解决方案将非常感激。

A class, or anything that you add with JavaScript does not persist on page refresh. 使用JavaScript添加的类或任何内容在页面刷新时不会保留。 That's just how it works. 这就是它的工作原理。

JavaScript is only modifying your locally loaded HTML elements and structure. JavaScript只修改本地加载的HTML元素和结构。 Once you refresh the page, or go to another page, all of that state is cleared out by the incoming page load. 刷新页面或转到另一个页面后,所有状态都会被传入的页面加载清除。 The server has no knowledge of anything you did on that page. 服务器不知道您在该页面上执行的任何操作。 It will send a new page with the default state, and any JavaScript will then run. 它将发送一个具有默认状态的新页面,然后将运行任何JavaScript。 This script also has no knowledge of any previous page loads or script execution. 此脚本也不知道任何先前的页面加载或脚本执行。

If you want state to persist across page loads, you need to do it at the server side, which involves making AJAX calls to the server to let it know what links you want to be "active", and then the server would be responsible for adding that class on subsequent page loads. 如果你希望状态在页面加载中持久化,你需要在服务器端执行它,这需要对服务器进行AJAX调用,让它知道你想要“活动”的链接,然后服务器将负责在后续页面加载时添加该类。

Inside of your each loop, this is actually the anchor tag not the tag that has the class 'classname'. 在每个循环内部, 实际上是锚标记而不是具有类'classname'的标记。 Your css should be: 你的CSS应该是:

.active {color:#ef9223;}

Also, you do not need the remove class if this code is being run on page load. 此外,如果在页面加载时运行此代码,则不需要remove类。

CHeck out this jsFiddle for an example - http://jsfiddle.net/XWLWL/7/ 查看这个jsFiddle的例子 - http://jsfiddle.net/XWLWL/7/

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

相关问题 打开/刷新页面时,设置按钮开始单击/激活 - A set button starts clicked/active when page is opened/refreshed 单击链接时如何在同一页面上显示另一个HTML文件的内容? - How to show another HTML file's contents on the same page when a link is clicked? 点击链接时需要显示弹出页面 - Need to show popup page when clicked the link 在页面加载时使链接处于活动状态,并使其保持活动状态,直到单击另一个指定的按钮为止 - make link active when page loads and force it ot remain active untill another specified button is clicked 当单击另一个选项卡(链接)并且用户留在同一页面上时,如何防止 CSS 发生变化? - How to prevent CSS from changing when another tab(link) is clicked and the user is remaining on the same page? 从其他页面单击链接时打开特定选项卡 - Open specific Tab when link is clicked from another page 单击“角度”中的“删除”按钮时页面未刷新 - Page not refreshed when clicked delete button in angular 单击链接或刷新页面后,jQuery toggleClass会丢失类 - Jquery toggleClass loses the class once links is clicked or page is refreshed 水平滚动页面链接无法在其他页面上使用 - Horizontal scrolling page links don't work from another page 页面加载时隐藏div,单击链接时显示 - Hide div when the page is loaded and show when link is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM