简体   繁体   English

获取jQuery UI选项卡中的当前选项卡

[英]get the current tab in jQuery UI tabs

I am using jQuery UI Tabs inside of the jQuery UI dialog window. 我在jQuery UI对话框窗口中使用jQuery UI选项卡。

I've come across an instance, where I need to find the id of the current tab when clicking on one of the dialog buttons. 我遇到过一个实例,我需要在单击其中一个对话框按钮时找到当前选项卡的ID。 Looking at the HTML generated by jQuery UI tabs and dialog, I can't really find a way of doing this. 看看jQuery UI选项卡和对话框生成的HTML,我真的找不到这样做的方法。 The <ul> elements that hold the tab, are about 3 <div> 's away from the group of dialog buttons. 保存选项卡的<ul>元素距离对话框按钮组约3 <div>

I tried: 我试过了:

$("#DialogBox").dialog({
    autoOpen: false,
    modal: true,
    buttons: [
        {
        text: "Save",
        click: function () {
        var currentTabId = $(this).closest("ul").attr("id");
        alert(currentTabId);

But I just get an 'undefined' alert back. 但我只是得到了一个“未定义”的警报。

Is there a way of doing this? 有办法做到这一点吗?

Thanks 谢谢

This is what worked for me (jQuery 1.9, jQueryUI 1.10). 这对我有用(jQuery 1.9,jQueryUI 1.10)。 I have not tested this for earlier versions of jQueryUI, but if you have jQueryUI 1.8 or earlier, instead of 'active' try using 'select'. 我没有为早期版本的jQueryUI测试过这个,但如果你有jQueryUI 1.8或更早版本,而不是'active'尝试使用'select'。

// GET INDEX OF ACTIVE TAB
// make sure to replace #tabs with the actual selector
// that you used to create the tabs
var activeTabIdx = $('#tabs').tabs('option','active');

// ID OF ACTIVE TAB
// make sure to change #tabs to your selector for tabs
var selector = '#tabs > ul > li';
var activeTabID = $(selector).eq(activeTabIdx).attr('id');

// ID OF ACTIVE TAB CONTENT
// make sure to change #tabs to your selector for tabs
var selector = '#tabs [id=ui-tabs-' + (activeTabIdx + 1) + ']';
var activeTabContentID = $(selector).attr('id');

在jQuery 1.9+的情况下使用以下内容,

var currentTabId = $('div[id="mytabs"] ul .ui-tabs-active').attr("id");

对于JQuery UI 1.11+,这对我有用

$("ul>.ui-tabs-active").attr('aria-controls');

⚡ worked for me this way ⚡ this以这种方式为我工作⚡

var currentTab=$("ul> .ui-tabs-active").attr('aria-controls');
console.log(currentTab);

here is the correct one and the simplest: 这是正确的,最简单的:

var active = $(".tab-pane.active").attr("id");
console.log(active);

You should add active selector next to tab-pane. 您应该在选项卡窗格旁边添加活动选择器。 This will return the current active tab ID. 这将返回当前活动的选项卡ID。

//for getting selected tabs //用于获取选定的标签

var tabs = $(" #tabs ").children().find(" .current ").attr( 'href' ); VAR选项卡= $( “#tabs ”)。孩子()找到(“ .current”).attr( 'href'属性 )。

对我有用的是:

var current_tab = $("#tabs .ui-state-active a").attr('href');

According to manual http://api.jqueryui.com/tabs/ getter of active JqueryUI tab is 根据手册http://api.jqueryui.com/tabs/ getter的活动JqueryUI选项卡是

var active = $( ".selector" ).tabs( "option", "active" );

*Replace ".selector" by your one. *替换你的".selector"

Then active.attr( 'id' ) will return exactly what you need. 然后active.attr( 'id' )将完全返回您需要的内容。

Asume that ui tabs container's id is tab-container. 假设ui tabs容器的id是tab-container。 Working snippet is 工作片段是

$('#tab-container').find('>div:nth-of-type(' + ($('#tab-container').tabs("option", "active") + 1) + ')'); 

Tested with jQuery UI v1.11.2, jQuery v1.11.3 and Chrome 45. 使用jQuery UI v1.11.2,jQuery v1.11.3和Chrome 45进行测试。

This also works, using JQuery 3.1.1 and Jquery UI 1.12.1, when you need to select your active tab in javascript (by "select" I mean in a JQuery selector, not "select" in the sense of making the tab active, since of course the tab is already active). 当你需要在javascript中选择你的活动标签时,这也可以使用JQuery 3.1.1和Jquery UI 1.12.1(通过“select”我的意思是在JQuery选择器中,而不是“选择”,意思是使标签处于活动状态,因为标签已经激活了)。

To get a reference to the currently selected tab, first get a reference to the active link (substitute the id of your tabs container "myTabs"): 要获取对当前所选选项卡的引用,首先获取对活动链接的引用(替换选项卡容器“myTabs”的id):

var $link = $('div[id=myTabs] ul .ui-tabs-active');

$link has the id the tab in the attribute "aria-controls": $ link的id为“aria-controls”属性中的tab:

var $tab = $('#' + $link.attr('aria-controls'));

$tab is a reference to the tab body. $ tab是对标签主体的引用。 For example, you could call 例如,你可以打电话

$tab.html('[html here]') 

to fill or replace the tab content. 填写或替换选项卡内容。

(This answer is relevant for JQuery UI 1.12 and probably a few versions before.) (这个答案与JQuery UI 1.12有关,之前可能有几个版本。)

It depends what you mean by tab... There is the thing that you click to select a tab and the panel that is displayed because of the click. 这取决于您对标签的含义...您可以单击以选择标签和由于单击而显示的面板。 The thing you click is a list item <li> that contains an anchor <a> tag with an href attribute that points to the panel id (it's prepended with a '#'). 你点击的是一个列表项<li> ,它包含一个锚<a>标签,其中一个href属性指向面板ID(它前面带有'#')。 The panel id and href values are set by you (not JQuery). 面板id和href值由您设置(而不是JQuery)。 The list item has no id by default, but the anchor element does... it is generated by JQuery and will be something like 'ui-id-88'. 列表项默认没有id,但是anchor元素确实...它是由JQuery生成的,就像'ui-id-88'。 To get either the tab id, anchor id or the panel id, you can use the following: 要获取选项卡ID,锚ID或面板ID,您可以使用以下命令:

// if you have nested tabs this might not work... in such case, give 
// your parent tab and panel a unique class and use it in selector
var $tabs = $("#tabs");
var tabIndex = $tabs.tabs("option", "active");
var $tab = $tabs.find("li[role=tab]").eq(tabIndex);
var tabId = $tab.attr("id"); // undefined unless set by user
var anchorId = $tab.attr("aria-labelledby"); // or $tabs.find("ul li a.ui-tabs-anchor").eq(tabIndex).attr("id");
var panelId = $tab.attr("aria-controls"); //or $tabs.find(".ui-tabs-panel").eq(tabIndex).attr("id");
// note: panelId will also be in href of anchor with prepended # sign
alert("tabId=" + tabId + ", anchorId=" + anchorId + ", panelId=" + panelId);

Here is one simple method. 这是一个简单的方法。

 var id = $(".tab-pane").attr("id");                   
 alert(id);

This will return the current tab ID. 这将返回当前标签ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM