簡體   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