頁面重新載入後維護選單狀態
要在頁面重新載入後保留選單狀態,您可以使用本機儲存。具體方法如下:
<code class="javascript">// Assuming you have identified the clicked menu link as `clickedLink` localStorage.setItem('menuState', clickedLink.id);</code>
<code class="javascript">const menuState = localStorage.getItem('menuState');</code>
<code class="javascript">if (menuState) { const clickedLink = document.getElementById(menuState); clickedLink.classList.add('clicked', 'hovered'); // Adjust translation and other styles accordingly. }</code>
<code class="javascript">// Assuming you have a variable `currentPage` to store the current page localStorage.setItem('pageState', currentPage); const pageState = localStorage.getItem('pageState'); if (pageState) { currentPage = pageState; // Adjust page content and navigation state accordingly. }</code>
<code class="javascript">localStorage.removeItem('menuState'); localStorage.removeItem('pageState');</code>
儲存位置的優點和缺點
本地儲存:
:
伺服器端儲存:
優點:
以上是如何使用儲存在頁面重新載入後維護選單和頁面狀態?的詳細內容。更多資訊請關注PHP中文網其他相關文章!