如何在页面刷新后保持当前选项卡处于活动状态,并在URL中使用哈希值。

huangapple go评论77阅读模式
英文:

How to keep current tab active after page refresh and using hash in the url

问题

如何在刷新页面时保存当前活动的选项卡?并且是否可以通过URL访问它,例如#contact-tab-pane

如果你想在刷新页面时保存当前活动的选项卡,你可以使用JavaScript来实现。你可以通过以下步骤来完成:

  1. 使用JavaScript获取当前活动的选项卡的ID。
  2. 使用localStorage将选项卡的ID保存在本地存储中。
  3. 在页面加载时,检查本地存储中是否有保存的选项卡ID,并将其设置为活动选项卡。

以下是一个简单的示例代码:

// 获取当前活动选项卡的ID
var activeTabId = $('#myTab .nav-link.active').attr('id');

// 保存选项卡ID到本地存储
localStorage.setItem('activeTabId', activeTabId);

// 在页面加载时,检查本地存储并设置活动选项卡
$(document).ready(function() {
  var savedTabId = localStorage.getItem('activeTabId');
  if (savedTabId) {
    $('#' + savedTabId).tab('show');
  }
});

关于通过URL访问选项卡,你可以使用锚点(anchor)来实现。只需在URL中包含选项卡的ID,例如#contact-tab-pane,然后使用JavaScript来解析URL并激活相应的选项卡。

希望这能帮助你实现所需的功能。

英文:

I have the following snippet:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65&quot; crossorigin=&quot;anonymous&quot;&gt; 
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js&quot; integrity=&quot;sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;ul class=&quot;nav nav-tabs&quot; id=&quot;myTab&quot; role=&quot;tablist&quot;&gt;
  &lt;li class=&quot;nav-item&quot; role=&quot;presentation&quot;&gt;
    &lt;button class=&quot;nav-link active&quot; id=&quot;home-tab&quot; data-bs-toggle=&quot;tab&quot; data-bs-target=&quot;#home-tab-pane&quot; type=&quot;button&quot; role=&quot;tab&quot; aria-controls=&quot;home-tab-pane&quot; aria-selected=&quot;true&quot;&gt;Home&lt;/button&gt;
  &lt;/li&gt;
  &lt;li class=&quot;nav-item&quot; role=&quot;presentation&quot;&gt;
    &lt;button class=&quot;nav-link&quot; id=&quot;profile-tab&quot; data-bs-toggle=&quot;tab&quot; data-bs-target=&quot;#profile-tab-pane&quot; type=&quot;button&quot; role=&quot;tab&quot; aria-controls=&quot;profile-tab-pane&quot; aria-selected=&quot;false&quot;&gt;Profile&lt;/button&gt;
  &lt;/li&gt;
  &lt;li class=&quot;nav-item&quot; role=&quot;presentation&quot;&gt;
    &lt;button class=&quot;nav-link&quot; id=&quot;contact-tab&quot; data-bs-toggle=&quot;tab&quot; data-bs-target=&quot;#contact-tab-pane&quot; type=&quot;button&quot; role=&quot;tab&quot; aria-controls=&quot;contact-tab-pane&quot; aria-selected=&quot;false&quot;&gt;Contact&lt;/button&gt;
  &lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;tab-content&quot; id=&quot;myTabContent&quot;&gt;
  &lt;div class=&quot;tab-pane fade show active&quot; id=&quot;home-tab-pane&quot; role=&quot;tabpanel&quot; aria-labelledby=&quot;home-tab&quot; tabindex=&quot;0&quot;&gt;Home&lt;/div&gt;
  &lt;div class=&quot;tab-pane fade&quot; id=&quot;profile-tab-pane&quot; role=&quot;tabpanel&quot; aria-labelledby=&quot;profile-tab&quot; tabindex=&quot;0&quot;&gt;Profile&lt;/div&gt;
  &lt;div class=&quot;tab-pane fade&quot; id=&quot;contact-tab-pane&quot; role=&quot;tabpanel&quot; aria-labelledby=&quot;contact-tab&quot; tabindex=&quot;0&quot;&gt;Contact&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

How can I save the current active tab if I refresh the page? And is it possible to access it through the URL like #contact-tab-pane ?

答案1

得分: 1

在这里,我们需要使用 window.location.hash; 读取当前的URL哈希值。

然后,我们需要为 [data-bs-toggle=&quot;tab&quot;] 创建一个 click 事件,使用 history.pushState(null, null, activeTabId); 将哈希值添加到当前的URL中。

尝试 JSfiddle

&lt;script&gt;
    $(document).ready(function ($) {
        var tabIdFromUrl = window.location.hash;
        if (tabIdFromUrl) {
            var activeTabBtn = $(&#39;[data-bs-target=&quot;&#39; + tabIdFromUrl + &#39;&quot;]&#39;);
            if (activeTabBtn) {
                var tab = new bootstrap.Tab(activeTabBtn);
                tab.show();
            }
        }
        $(document).on(&#39;click&#39;, &#39;[data-bs-toggle=&quot;tab&quot;]&#39;, function (event) {
            var activeTabId = $(this).attr(&#39;data-bs-target&#39;);
            history.pushState(null, null, activeTabId);
        });
    });
&lt;/script&gt;
英文:

Here we need to read current url hash value with window.location.hash;

And then we need to create click event for [data-bs-toggle=&quot;tab&quot;] to push the hash value to the current url using history.pushState(null, null, activeTabId);

Try JSfiddle

&lt;script&gt;
    $(document).ready(function ($) {
        var tabIdFromUrl = window.location.hash;
        if (tabIdFromUrl) {
            var activeTabBtn = $(&#39;[data-bs-target=&quot;&#39; + tabIdFromUrl + &#39;&quot;]&#39;);
            if (activeTabBtn) {
                var tab = new bootstrap.Tab(activeTabBtn);
                tab.show();
            }
        }
        $(document).on(&#39;click&#39;, &#39;[data-bs-toggle=&quot;tab&quot;]&#39;, function (event) {
            var activeTabId = $(this).attr(&#39;data-bs-target&#39;);
            history.pushState(null, null, activeTabId);
        });
    });
&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年5月17日 15:24:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定