繁体   English   中英

当它是当前页面时,如何使当前的导航链接也处于活动状态?

[英]How do i make current nav link which is also home page active when it's the current page?

所以我有一个可行的解决方案,直到我决定将我的主页(因为它只是一个空页面)更改为我的导航链接之一。 现在,当我单击导航链接时,它会将我重定向到没有 url 的主页,因此无法找到链接。 这是我之前使用的代码:

    var activePage = window.location.pathname;
        
        const navLinks = document.querySelectorAll("nav a").
            forEach(link => {        
                    if (link.href.includes(activePage)) {
                        link.classList.add("current");
        }   });

这是我试过的

var activePage = window.location.pathname;
if (activePage == "/") {
    activePage="/ProjectsList"
}
var d = document.getElementById("navLink3");
/*d.className += "current";*/
const navLinks = document.querySelectorAll("nav a").
    forEach(link => {
     
            if (activePage == "/AdminPage" || activePage == "/Account/UserProfile") {
            if (link.href.includes(activePage)) {
                link.classList.add("current");
                }
            }
            else {
                d.className += "current";
            console.log("wok")}
        
        
        
    });

它在技术上达到了 else 语句,但该类没有被应用。 我想提一下,我使用 asp.net 并且我通过在导航页面的 .cshtml 中写入来更改索引页面@page "/"什么帮助吗?

你能做这个吗:

d.className = "current";
Instead of:-
d.className += "current";

暂无
暂无

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

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