jQuery UI 标签:获取当前标签索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8020884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 09:54:04  来源:igfitidea点击:

jQuery UI tabs: get current tab index

jqueryjquery-uitabs

提问by Matteo Pagliazzi

I want to get the index of the current tab using jQuery UItabs: especially when the showor selectevents are fired i want to know the tab thay are referred is this possible?

我想使用jQuery UI选项卡获取当前选项卡的索引:尤其是在触发showselect事件时,我想知道引用的选项卡这可能吗?

回答by YogeshWaran

You can use this to find

您可以使用它来查找

var $tabs = $('#tab').tabs();
var selected = $tabs.tabs('option', 'selected');

From JQuery 1.9 on

从 JQuery 1.9 开始

var $tabs = $('#tab').tabs();
var selected = $tabs.tabs('option', 'active');

回答by Shef

$('#tabs').tabs({
    select: function(event, ui) { // select event
        $(ui.tab); // the tab selected
        ui.index; // zero-based index
    },
    show: function(event, ui) { // show event
        $(ui.tab); // the tab shown
        ui.index; // zero-based index
    }
});

Demo

演示



Or, if you don't want to bind the event listeners on the initialization you can bind them like this:

或者,如果您不想在初始化时绑定事件侦听器,您可以像这样绑定它们:

$('#tabs')
    .bind('tabsselect', function(event, ui) { // select event
        $(ui.tab); // the tab selected
        ui.index; // zero-based index
    })
    bind('tabsshow'. function(event, ui) { // show event
        $(ui.tab); // the tab shown
        ui.index; // zero-based index
    });

回答by C???

I just implemented this in one of my projects:

我刚刚在我的一个项目中实现了这一点:

var lastTab = 0; // global variable

$(function() {
    $('#tabs').tabs({ 
        select: function(event, ui) { 
            lastTab = ui.index; 
        } 
    });
});

And then anywhere else in your code you can simply reference lastTab.

然后您可以在代码中的任何其他地方简单地引用lastTab.

回答by Vincent

For jQuery 1.9 or newer...

对于 jQuery 1.9 或更新版本...

$('#tabs').tabs({
    activate: function(event, ui) {
        ui.newTab.index(); // zero-based index
    }
});