Remembering jQuery tab position after page reloads

被刻印的时光 ゝ 提交于 2020-01-15 06:46:18

问题


I have a page that contains some GridViews. I have kept them in tabs using tab-menu. There are four tabs.

My problem is when the page reloads, the tab gets reset to the first tab.

HTML:

<div id="tabbed_box_1" class="tabbed_box">
    &nbsp; &nbsp;
    <div class="tabbed_area">
        <ul class="tabs">
            <li><a href="#" title="content_1" class="tab active">Bottom Banner</a></li>
            <li><a href="#" title="content_2" class="tab">Side Banner Bottom</a></li>
            <li><a href="#" title="content_3" class="tab">Side Banner Top</a></li>
            <li><a href="#" title="content_4" class="tab">Main Ad</a></li>
        </ul>
        <div id="content_1" class="content">
            <table width="500" border="0" align="center" cellpadding="0" cellspacing="2" class="border">
                <tr>
                    <td align="center">
                      //some gridview here
                    </td>
                </tr>
            </table>
        </div>
        //similarly three more gridviews

jQuery:

 $(document).ready(function () {

     // When a link is clicked
     $("a.tab").click(function () {

        // switch all tabs off
        $(".active").removeClass("active");

        // switch this tab on
        $(this).addClass("active");

        // slide all content up
        $(".content").slideUp();

        // slide this content up
        var content_show = $(this).attr("title");
        $("#" + content_show).slideDown();

    });
});

How can I make the currently-clicked tab show even after page reloads?


回答1:


The simple solution is to use localStorage to persist the selection :

 $(document).ready(function () {

        function activate(tab) {
           // switch all tabs off
            $(".active").removeClass("active");

            // switch this tab on
            tab.addClass("active");

            // slide all content up
            $(".content").slideUp();

            // slide this content up
            var content_show = tab.attr("title");
            $("#" + content_show).slideDown();
        }

        if (localStorage) { // let's not crash if some user has IE7
             var index = parseInt(localStorage['tab']||'0');
             activate($('a.tab').eq(index));
        }

        // When a link is clicked
        $("a.tab").click(function () {
            if (localStorage) localStorage['tab'] = $(this).closest('li').index();
            activate($(this));
        });

    });



回答2:


You can try using an hashtag in your URL whenever a tab is clicked, and then in your code, you check if there exists an hashtag, which id of which tab corresponds it to.

<ul id="tabs">
    <li><a id="category1" href="#category1">Category 1</a></li>
    <li><a id="category2" href="#category2">Category 2</a></li>
    <li><a id="category3" href="#category3">Category 3</a></li>
</ul>

if(window.location.hash) {
    $(window.location.hash).click();
}

based on: Jquery, activate script if hashtag on end of URL matches a class name



来源:https://stackoverflow.com/questions/16054274/remembering-jquery-tab-position-after-page-reloads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!