开发者

In jQuery UI tabs, how do I get the tab element by its tab index?

开发者 https://www.devze.com 2023-03-16 17:42 出处:网络
I have the following JavaScript function to select a tab by its index: function changeTab(tabIndex) { $(\"#panel\").tabs(\"select\", tabIndex);

I have the following JavaScript function to select a tab by its index:

function changeTab(tabIndex) {
    $("#panel").tabs("select", tabIndex);
}

But I have to check if that tab exists and if it's visible, or else it would try to select a tab that shouldn't be available.

For the existence it's fine, I just have to use $("#panel").开发者_运维问答tabs("length");, but to check if it is visible I would need the element itself.


It seems there isn't a built-in method for that, but I was able to accomplish it by navigating to the tab:

function changeTab(tabIndex) {
    var panel = $("#panel");
    var queryIndex = tabIndex + 1; // 1 based
    var desiredTab = panel.find("> ul li:nth-child( " + queryIndex + ")");

    if (desiredTab && desiredTab.is(":visible")) {
        panel.tabs("select", tabIndex);
    }
}


I haven't tested but you should be able to get the index of the selected tab by

var activeIndex = $("#panel").tabs( "option" , 'selected')

And then you should be able to just very that activeIndex equals tabIndex


First get the Active index

var activeIndex = $("#panel").tabs('option', 'active');

Then using the css class get the tab content panel

// this will return the html element
var element=   $("#panel").find( ".ui-tabs-panel" )[activeIndex]; 

now wrapped it in jQuery object to further use it

 var tabContent$ = $(element);

here i want to add two info the class .ui-tabs-nav is for Navigation associated with and .ui-tabs-panel is associated with tab content panel. in this link demo in jquery ui website you will see this class is used - http://jqueryui.com/tabs/#manipulation

0

精彩评论

暂无评论...
验证码 换一张
取 消