请记住刷新后哪个选项卡处于活动状

the*_*ner 51 jquery jquery-plugins

我正在网页上使用jquery选项卡,当页面刷新时,它会丢失我曾经使用过的标签并返回第一个标签页.

有没有人遇到这个问题,知道如何解决它?

小智 38

和其他人一样,我在jQueryUI 1.10中苦苦寻找我的ui-tabs cookie历史.感谢Harry的解决方案以及我在下面的代码中提到的其他一些在线文档,我现在有了一个有效的非cookie解决方案!我能够在Firefox 18.0.1和IE 9.0.12中进行测试.根据我的资源,Chrome,Firefox,Safari和IE8及以上版本支持会话存储.

  $(function() {
    //  jQueryUI 1.10 and HTML5 ready
    //      http://jqueryui.com/upgrade-guide/1.10/#removed-cookie-option 
    //  Documentation
    //      http://api.jqueryui.com/tabs/#option-active
    //      http://api.jqueryui.com/tabs/#event-activate
    //      http://balaarjunan.wordpress.com/2010/11/10/html5-session-storage-key-things-to-consider/
    //
    //  Define friendly index name
    var index = 'key';
    //  Define friendly data store name
    var dataStore = window.sessionStorage;
    //  Start magic!
    try {
        // getter: Fetch previous value
        var oldIndex = dataStore.getItem(index);
    } catch(e) {
        // getter: Always default to first tab in error state
        var oldIndex = 0;
    }
    $('#tabs').tabs({
        // The zero-based index of the panel that is active (open)
        active : oldIndex,
        // Triggered after a tab has been activated
        activate : function( event, ui ){
            //  Get future value
            var newIndex = ui.newTab.parent().children().index(ui.newTab);
            //  Set future value
            dataStore.setItem( index, newIndex ) 
        }
    }); 
    }); 
Run Code Online (Sandbox Code Playgroud)

  • 到目前为止最好的方法,我只是想向新的 Jquery 用户指出一些事情:行 '$(function() {' 与 '$(document).ready(function () {' 相同)行,这意味着每次进入页面时都会执行您提供的代码,但要在 DOM 完全加载之后执行。 (2认同)

taw*_*kov 21

我假设您正在使用jQuery UI选项卡,

以下是使用制表符+ Cookie保存最后点击的标签的示例

http://jqueryui.com/demos/tabs/#cookie

演示:打开此链接 http://jqueryui.com/demos/tabs/cookie.html

关闭它并重新打开它,您将看到相同的单击选项卡

更新: 经过3年的回答,jquery ui已经弃用了cookie选项:http://jqueryui.com/upgrade-guide/1.9/#deprecated-cookie-option.

但如果这符合您的需求,您仍然可以在这里查看/sf/answers/1001932081/

  • http://jqueryui.com/demos/tabs/cookie.html链接已损坏.我认为其他链接不再显示您想要的内容.也许我错了......? (3认同)
  • @tawfekov它仍然是一个链接答案. (3认同)

小智 11

我刚刚实施的一个更简单的替代方案:

$(".tabs").tabs({
    select: function(event, ui) {                   
       window.location.replace(ui.tab.hash);
    },
});
Run Code Online (Sandbox Code Playgroud)

这会将哈希值添加到网址,以便页面刷新将重新加载该选项卡,并且通过使用location.replace而不是location.hash,我们不会用不需要的步骤填充用户的历史记录.

希望有所帮助.

  • 此方法不起作用.它实际上没有做任何事情.它不会替换URL的任何部分,也不会在页面重新加载时强制执行活动选项卡. (3认同)
  • 您的方法很棒,但重新加载页面.window.location.hash = ui.tab.hash; 没有重新加载页面做同样的工作. (2认同)

Har*_*rry 5

现在cookie已经在jQuery UI 1.10.0中消失了,我将Gayathiri的方法与新的选项和事件混合在一起:

// using cookie plugin from https://github.com/carhartl/jquery-cookie

var tabCookieName = "ui-tabs-1";
$(".tabs").tabs({
    active : ($.cookie(tabCookieName) || 0);
    activate : function( event, ui ) {
        var newIndex = ui.newTab.parent().children().index(ui.newTab);
        // my setup requires the custom path, yours may not
        $.cookie(tabCookieName, newIndex, { path: window.location.pathname });
    }
});
Run Code Online (Sandbox Code Playgroud)