如何在页面重新加载时记住类的活动状态

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

How to remember class active on page reload

问题

I'm new, please tell me if it's possible to do this: there are navigation tabs on the page, I want the selected tabs to be active when the page is reloaded. So that the class="nav-link active" is remembered before reloading the page and automatically applied after reloading.

P.s. while compiling the question, I came across sessionStorage, judging by the description, this is exactly what I need, explain to me, please, how can I apply this to my example?

<nav>
    <div class="nav nav-tabs justify-content-center" role="tablist">
        {% for region in regions %}
            <button class="nav-link" data-bs-toggle="tab" data-bs-target="#region-{{ department.id }}" type="button" role="tab" aria-controls="region-{{ department.id }}">{{ department.department_name }}</button>
        {% endfor %}
    </div>
</nav>
<div class="tab-content">
    {% for region in regions %}
        <div class="tab-pane fade" id="region-{{ department.id }}" role="tabpanel"></div>
    {% endfor %}
</div>

I try like this, but only one value is saved and does not change when clicking on other tabs:

$(function($){
    var storage = sessionStorage.getItem('nav-tabs');
    if (storage && storage !== "#") {
        $('.nav-tabs button[data-bs-target="' + storage + '"]').tab('show');
    }

    $('div.nav').on('click', function() {
        var id = $(this).find('button').attr('data-bs-target');
        sessionStorage.setItem('nav-tabs', id);
    });
});

如何在页面重新加载时记住类的活动状态

英文:

I'm new, please tell me if it's possible to do this: there are navigation tabs on the page, I want the selected tabs to be active when the page is reloaded. So that the class=&quot;nav-link active&quot; is remembered before reloading the page and automatically applied after reloading.

P.s. while compiling the question, I came across sessionStorage, judging by the description, this is exactly what I need, explain to me, please, how can I apply this to my example?

    &lt;nav&gt;
       &lt;div class=&quot;nav nav-tabs justify-content-center&quot; role=&quot;tablist&quot;&gt;
{% for region in regions %} 
            &lt;button class=&quot;nav-link&quot; data-bs-toggle=&quot;tab&quot; data-bs-target=&quot;#region-{{ department.id }}&quot; type=&quot;button&quot; role=&quot;tab&quot; aria-controls=&quot;region-{{ department.id }}&quot;&gt;{{ department.department_name }}&lt;/button&gt;
{% endfor %}
       &lt;/div&gt;
    &lt;/nav&gt;
    &lt;div class=&quot;tab-content&quot;&gt;
{% for region in regions %} 
        &lt;div class=&quot;tab-pane fade&quot; id=&quot;region-{{ department.id }}&quot; role=&quot;tabpanel&quot;&gt;&lt;/div&gt;
{% endfor %}
    &lt;/div&gt;

I try like this, but only one value is saved and does not change when clicking on other tabs:

$(function($){
    var storage = sessionStorage.getItem(&#39;nav-tabs&#39;);
	if (storage &amp;&amp; storage !== &quot;#&quot;) {
		$(&#39;.nav-tabs button[data-bs-target=&quot;&#39; + storage + &#39;&quot;]&#39;).tab(&#39;show&#39;);
	}

	$(&#39;div.nav&#39;).on(&#39;click&#39;, function() {
		var id = $(this).find(&#39;button&#39;).attr(&#39;data-bs-target&#39;);
		sessionStorage.setItem(&#39;nav-tabs&#39;, id);
	});
});

如何在页面重新加载时记住类的活动状态

答案1

得分: 0

我找到了!

对于我的示例:

$(function($){
    var storage = sessionStorage.getItem('nav-tabs');
    if (storage && storage !== "#") {
        $('.nav-tabs button[data-bs-target="' + storage + '"]').tab('show');
    }

    $('div.nav').on('click', function() {
        sessionStorage.clear();
        var id = $(this).find('button.active').attr('data-bs-target');
        sessionStorage.setItem('nav-tabs', id);
    });
});

请注意,我只翻译了代码部分,没有包含其他内容。

英文:

I found!
To my example:

$(function($){
    var storage = sessionStorage.getItem(&#39;nav-tabs&#39;);
	if (storage &amp;&amp; storage !== &quot;#&quot;) {
		$(&#39;.nav-tabs button[data-bs-target=&quot;&#39; + storage + &#39;&quot;]&#39;).tab(&#39;show&#39;);
	}

	$(&#39;div.nav&#39;).on(&#39;click&#39;, function() {
	    sessionStorage.clear();
		var id = $(this).find(&#39;button.active&#39;).attr(&#39;data-bs-target&#39;);
		sessionStorage.setItem(&#39;nav-tabs&#39;, id);
	});
});

huangapple
  • 本文由 发表于 2023年6月1日 13:34:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378914.html
匿名

发表评论

匿名网友

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

确定