建立動態 Web 應用程式時,使用者介面 (UI) 需要提供直覺的導航體驗。無論是具有多個產品類別的電子商務網站還是內容豐富的管理儀表板,擁有活動狀態和可擴展菜單都可以增強可用性。在這篇文章中,我們將逐步建立一個 JavaScript 腳本,該腳本動態突出顯示導覽中的當前頁面,並根據使用者的路徑展開相關部分。
問題
當使用者瀏覽多層選單時,我們希望:
- 目前頁面連結上的活動狀態。
- 可擴充部分,如果使用者位於嵌套在選單中的頁面上,則會自動開啟。
讓我們看一個範例 HTML 結構以及如何新增 JavaScript 以使我們的選單變得聰明。
選單結構範例
這是一個典型的巢狀選單結構。我們將為選單中的每個項目使用 menu-item,為連結使用 menu-link,為可折疊子選單使用 menu-sub。
<!-- Categories -->
In this structure:
- Each main menu link (menu-toggle) opens a submenu when clicked.
- The actual pages are in the submenu links (menu-link).
The JavaScript Solution
We’ll use JavaScript to:
- Identify the current page.
- Apply an active class to the link that matches the current URL.
- Add an open class to the parent menu if the link is inside a collapsed submenu.
Here’s the JavaScript code:
<script> window.onload = function () { const currentPath = window.location.pathname; // Get the current path const links = document.querySelectorAll('.menu-link'); // Select all menu links links.forEach(function (link) { // Check if the link's href matches the current page's path if (link.getAttribute('href') === currentPath) { // Add 'active' class to the parent 'li' element of the link const menuItem = link.closest('.menu-item'); if (menuItem) { menuItem.classList.add('active'); // Check if the link is within a 'menu-sub', expand the parent menu const parentMenu = menuItem.closest('.menu-sub'); if (parentMenu) { // Add 'open' class to the parent 'menu-item' of the submenu const parentMenuItem = parentMenu.closest('.menu-item'); if (parentMenuItem) { parentMenuItem.classList.add('open'); } } } } }); }; </script>
分解代碼
- 取得目前路徑:
const currentPath = window.location.pathname;
這會取得目前頁面的路徑(例如,/inventory-issues),我們將用它來與選單中每個連結的 href 進行比較。
- 選擇選單連結:
const links = document.querySelectorAll('.menu-link');
我們選擇具有選單連結類別的所有元素,假設這些元素包含指向網站各個部分的連結。
- 符合目前頁面:
if (link.getAttribute('href') === currentPath) {
對於每個鏈接,我們檢查其 href 是否與 currentPath 匹配。如果是,則該連結適用於目前頁面。
- 設定活動狀態:
menuItem.classList.add('active');
我們為最近的選單項目新增一個活動類,允許我們設定活動頁面連結的樣式。
- 展開父選單:
const parentMenuItem = parentMenu.closest('.menu-item'); parentMenuItem.classList.add('open');
如果活動連結位於子選單 (menu-sub) 內,這部分程式碼將尋找包含該子選單的父選單項目並新增開放類,將其展開。
為活動和開放狀態添加 CSS
您需要為 CSS 中的活動類別和開放類別定義樣式:
<!-- Categories -->
In this structure:
- Each main menu link (menu-toggle) opens a submenu when clicked.
- The actual pages are in the submenu links (menu-link).
The JavaScript Solution
We’ll use JavaScript to:
- Identify the current page.
- Apply an active class to the link that matches the current URL.
- Add an open class to the parent menu if the link is inside a collapsed submenu.
Here’s the JavaScript code:
<script> window.onload = function () { const currentPath = window.location.pathname; // Get the current path const links = document.querySelectorAll('.menu-link'); // Select all menu links links.forEach(function (link) { // Check if the link's href matches the current page's path if (link.getAttribute('href') === currentPath) { // Add 'active' class to the parent 'li' element of the link const menuItem = link.closest('.menu-item'); if (menuItem) { menuItem.classList.add('active'); // Check if the link is within a 'menu-sub', expand the parent menu const parentMenu = menuItem.closest('.menu-sub'); if (parentMenu) { // Add 'open' class to the parent 'menu-item' of the submenu const parentMenuItem = parentMenu.closest('.menu-item'); if (parentMenuItem) { parentMenuItem.classList.add('open'); } } } } }); }; </script>
這種方法的好處
- 自動活動狀態:無需在每個頁面上對活動連結進行硬編碼。該腳本動態更新活動連結。
- 可擴充選單:使用者只能看到與目前頁面相關的部分,減少手動開啟選單的需要。
- 可重複使用:此腳本足夠通用,可以與各種巢狀選單結構一起使用,使其適應多種類型的網站。
?來自埃迪古萊
以上是為活動狀態和可擴展選單建立動態導航腳本的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python和JavaScript的主要區別在於類型系統和應用場景。 1.Python使用動態類型,適合科學計算和數據分析。 2.JavaScript採用弱類型,廣泛用於前端和全棧開發。兩者在異步編程和性能優化上各有優勢,選擇時應根據項目需求決定。

選擇Python還是JavaScript取決於項目類型:1)數據科學和自動化任務選擇Python;2)前端和全棧開發選擇JavaScript。 Python因其在數據處理和自動化方面的強大庫而備受青睞,而JavaScript則因其在網頁交互和全棧開發中的優勢而不可或缺。

Python和JavaScript各有優勢,選擇取決於項目需求和個人偏好。 1.Python易學,語法簡潔,適用於數據科學和後端開發,但執行速度較慢。 2.JavaScript在前端開發中無處不在,異步編程能力強,Node.js使其適用於全棧開發,但語法可能複雜且易出錯。

javascriptisnotbuiltoncorc; sanInterpretedlanguagethatrunsonenginesoftenwritteninc.1)JavascriptwasdesignedAsignedAsalightWeight,drackendedlanguageforwebbrowsers.2)Enginesevolvedfromsimpleterterpretpretpretpretpreterterpretpretpretpretpretpretpretpretpretcompilerers,典型地,替代品。

JavaScript可用於前端和後端開發。前端通過DOM操作增強用戶體驗,後端通過Node.js處理服務器任務。 1.前端示例:改變網頁文本內容。 2.後端示例:創建Node.js服務器。

選擇Python還是JavaScript應基於職業發展、學習曲線和生態系統:1)職業發展:Python適合數據科學和後端開發,JavaScript適合前端和全棧開發。 2)學習曲線:Python語法簡潔,適合初學者;JavaScript語法靈活。 3)生態系統:Python有豐富的科學計算庫,JavaScript有強大的前端框架。

JavaScript框架的強大之處在於簡化開發、提升用戶體驗和應用性能。選擇框架時應考慮:1.項目規模和復雜度,2.團隊經驗,3.生態系統和社區支持。

引言我知道你可能會覺得奇怪,JavaScript、C 和瀏覽器之間到底有什麼關係?它們之間看似毫無關聯,但實際上,它們在現代網絡開發中扮演著非常重要的角色。今天我們就來深入探討一下這三者之間的緊密聯繫。通過這篇文章,你將了解到JavaScript如何在瀏覽器中運行,C 在瀏覽器引擎中的作用,以及它們如何共同推動網頁的渲染和交互。 JavaScript與瀏覽器的關係我們都知道,JavaScript是前端開發的核心語言,它直接在瀏覽器中運行,讓網頁變得生動有趣。你是否曾經想過,為什麼JavaScr


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

Dreamweaver Mac版
視覺化網頁開發工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具