Javascript 刷新时保持在同一选项卡上(选项卡中的选项卡)

Javascript 刷新时保持在同一选项卡上(选项卡中的选项卡),javascript,jquery,html,tabs,Javascript,Jquery,Html,Tabs,页面提交时,主选项卡(注册、锁定、解锁等)保持在当前选项卡上,但子选项卡(列表、锁定、签出等)在页面提交时变为默认(搜索)。例如,如果我在列表或锁定选项卡上提交,它默认为搜索选项卡。 问题:我必须跟踪水平选项卡和垂直选项卡,目前我只能跟踪水平选项卡 HTML主选项卡(水平): jQuery子选项卡: $( "#tabs1" ).tabs().addClass( "ui-tabs-vertical ui-helper-clearfix" ); $( "#tabs1 li" ).removeCla

页面提交时,主选项卡(注册、锁定、解锁等)保持在当前选项卡上,但子选项卡(列表、锁定、签出等)在页面提交时变为默认(搜索)。例如,如果我在列表或锁定选项卡上提交,它默认为搜索选项卡。 问题:我必须跟踪水平选项卡和垂直选项卡,目前我只能跟踪水平选项卡

HTML主选项卡(水平):

jQuery子选项卡:

$( "#tabs1" ).tabs().addClass( "ui-tabs-vertical ui-helper-clearfix" );
$( "#tabs1 li" ).removeClass( "ui-corner-top" ).addClass( "ui-corner-left" );

我假设您使用的是jQuery UI选项卡

下面是一个使用tabs+Cookie保存上次单击的选项卡的示例

演示:打开这个链接

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

更新:在回答此问题3年后,jquery ui不推荐使用cookie选项:

但如果这符合您的需要,您仍然可以在这里查看

你可以试试这个

$(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 ) 
        }
    }); 
    }); 

我假设您使用的是jQuery UI选项卡

下面是一个使用tabs+Cookie保存上次单击的选项卡的示例

演示:打开这个链接

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

更新:在回答此问题3年后,jquery ui不推荐使用cookie选项:

但如果这符合您的需要,您仍然可以在这里查看

你可以试试这个

$(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 ) 
        }
    }); 
    }); 
已解决: 水平选项卡工作正常,但对于垂直选项卡,控件将转到默认选项卡,因此必须使用ui.oldtab.index()

jQuery:

var index1 = 'ui.oldTab.index()';
//  Define data store name
var dataStore = window.sessionStorage;
var oldIndex = 0;
try {
     // getter: Fetch previous value
     oldIndex1 = dataStore.getItem(index1);
     } catch(e) {}
$( "#tabs1" ).tabs({
active: oldIndex1,
activate: function(event, ui) {
//  Get future value
var newIndex1 = ui.newTab.parent().children().index(ui.newTab);
//  Set future value
try {
     dataStore.setItem( index1, newIndex1);
     } catch(e) {}
     }
});
已解决: 水平选项卡工作正常,但对于垂直选项卡,控件将转到默认选项卡,因此必须使用ui.oldtab.index()

jQuery:

var index1 = 'ui.oldTab.index()';
//  Define data store name
var dataStore = window.sessionStorage;
var oldIndex = 0;
try {
     // getter: Fetch previous value
     oldIndex1 = dataStore.getItem(index1);
     } catch(e) {}
$( "#tabs1" ).tabs({
active: oldIndex1,
activate: function(event, ui) {
//  Get future value
var newIndex1 = ui.newTab.parent().children().index(ui.newTab);
//  Set future value
try {
     dataStore.setItem( index1, newIndex1);
     } catch(e) {}
     }
});

它适用于水平选项卡,但同样的逻辑不适用于垂直选项卡。另外请注意,我必须同时跟踪水平选项卡和垂直选项卡。这对水平选项卡有效,但对垂直选项卡不起作用。另外请注意,我必须同时跟踪水平选项卡和垂直选项卡。这就是我目前使用的水平选项卡,它正在工作。但同样的逻辑不适用于垂直选项卡,这是我目前用于水平选项卡的逻辑,并且它正在工作。但同样的逻辑不适用于垂直选项卡
var index1 = 'ui.oldTab.index()';
//  Define data store name
var dataStore = window.sessionStorage;
var oldIndex = 0;
try {
     // getter: Fetch previous value
     oldIndex1 = dataStore.getItem(index1);
     } catch(e) {}
$( "#tabs1" ).tabs({
active: oldIndex1,
activate: function(event, ui) {
//  Get future value
var newIndex1 = ui.newTab.parent().children().index(ui.newTab);
//  Set future value
try {
     dataStore.setItem( index1, newIndex1);
     } catch(e) {}
     }
});