需要 jQuery 选项卡当前选定的选项卡 ID

发布于 2024-08-14 11:04:13 字数 192 浏览 13 评论 0原文

我知道我可以获取当前所选选项卡的索引,但是我可以以某种方式获取 ID(相当于 ui.panel.id,如果这是由选项卡事件触发的...但它是不是)当前选定的选项卡?我不想使用索引,因为选项卡的顺序可能会改变。我不想使用样式标记,因为这些标记可能在未来的版本中发生变化。有没有办法呢?如果没有,我可以以某种方式使用索引来访问它(甚至可以先访问面板对象)吗?还有其他想法吗?

I know I can get the index of the currently selected tab but can I somehow get to the ID (the equivalent of the ui.panel.id if this were triggered by an tab event...but it's not) of the currently selected tab? I'd prefer not to use the index because ordering of the tabs might change. I prefer not to use the style markups as those may change in future releases. Is there a method for this? If not, can I somehow use the index to access this (maybe even by accessing the panel object first)? Any other ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

遇到 2024-08-21 11:04:13

您可以使用 :visible 伪选择器 来定位可见面板:

$("#tabs .ui-tabs-panel:visible").attr("id");

值得注意的是,您可以从 activate 检索活动选项卡事件

$("#tabs").tabs({
    activate: function (event, ui) {
        console.log(ui.newPanel[0].id);
    }
});

You can use the :visible pseudo-selector to target the visible panel:

$("#tabs .ui-tabs-panel:visible").attr("id");

It's worth noting that you can retrieve the active tab from the activate event:

$("#tabs").tabs({
    activate: function (event, ui) {
        console.log(ui.newPanel[0].id);
    }
});
北风几吹夏 2024-08-21 11:04:13

乔纳森·桑普森的答案不再有效。尝试...

$("#tabs .ui-tabs-panel:visible").attr("id");

jsFiddle: http://jsfiddle.net/tbEq6/

Jonathan Sampson's answer doesn't work anymore. Try...

$("#tabs .ui-tabs-panel:visible").attr("id");

jsFiddle: http://jsfiddle.net/tbEq6/

土豪我们做朋友吧 2024-08-21 11:04:13

如果您想要从所选选项卡中获取 id(或者实际上是 href),您可以使用 eq() 来检索 jQuery 对象。

您可以在此处查看示例:http://jsfiddle.net/svirkant/hpU3T/1/

If you want the id (or actually the href) from the selected tab, you can use eq() to retrieve the jQuery Object.

You can see an example here: http://jsfiddle.net/svierkant/hpU3T/1/

辞别 2024-08-21 11:04:13

正如我在这个问题的回答中发布的那样,有多种方法可以实现这一目标。

jQuery 文档上,他们建议执行以下操作来查找索引当前打开的选项卡:

var $tabs = $('#example').tabs();
var selected = $tabs.tabs('option', 'selected'); // => 0

但是,如果您需要使用该选项卡执行大量操作,则这是不切实际的。
为什么他们还没有提供获取实际元素的更实用的解决方案,我不确定,但是,通过使用 jQuery,您可以自己创建一个简单的解决方案。

在下面的代码中,我将向您展示使用当前选项卡执行您想要的任何操作是多么容易:

var curTab = $('.ui-tabs-panel:not(.ui-tabs-hide)'),
    curTabIndex = curTab.index(),  //  will get you the index number of where it sits
    curTabID = curTab.prop("id"),  //  will give you the id of the tab open if existant
    curTabCls = curTab.attr("class");  //  will give you an array of classes on this tab
        //  etc ....
//    now, if you wanted a little more depth, for instance specific tabs area (if you have multiple tabs on your page) you can do simply add to your selector statement
var curTab = $('#myTabs_1 .ui-tabs-panel:not(.ui-tabs-hide)');
//    then you can make simple calls to that tab and get whatever data or manipulate it how you please
curTab.css("background-color", "#FFF");

As I posted in an answer to this question, there are several ways to achieve this.

On the jQuery documents, they propose to do the following to find the index of the currently open tab:

var $tabs = $('#example').tabs();
var selected = $tabs.tabs('option', 'selected'); // => 0

However, this is impractical if you need to do a lot with that tab.
Why they don't yet provide a more practical solution of getting the actual element, I'm unsure, however, through use of jQuery there is an easy solution you can create yourself.

In the following code i'll show you just how easy it is to do anything you want with the current tab:

var curTab = $('.ui-tabs-panel:not(.ui-tabs-hide)'),
    curTabIndex = curTab.index(),  //  will get you the index number of where it sits
    curTabID = curTab.prop("id"),  //  will give you the id of the tab open if existant
    curTabCls = curTab.attr("class");  //  will give you an array of classes on this tab
        //  etc ....
//    now, if you wanted a little more depth, for instance specific tabs area (if you have multiple tabs on your page) you can do simply add to your selector statement
var curTab = $('#myTabs_1 .ui-tabs-panel:not(.ui-tabs-hide)');
//    then you can make simple calls to that tab and get whatever data or manipulate it how you please
curTab.css("background-color", "#FFF");
橘亓 2024-08-21 11:04:13

JQuery 1.9 selected 被弃用后

,因此使用

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

After JQuery 1.9 selected is deprecated

So use

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

以可爱出名 2024-08-21 11:04:13

对于 1.9 以下的 jquery 版本:

 <div class="roundedFloatmenu">
        <ul id="unid">
            <li class="titleHover" id="li_search">Search</li>               
            <li class="titleHover" id="li_notes">Notes</li>
             <li class="titleHover active" id="li_writeback">Writeback</li>         
            <li class="titleHover" id="li_attorney">Attorney</li>
        </ul>
    </div 

您可以使用以下命令找到活动选项卡:

 jQuery('#unid').find('li.active').attr('id')

For jquery version below 1.9:

 <div class="roundedFloatmenu">
        <ul id="unid">
            <li class="titleHover" id="li_search">Search</li>               
            <li class="titleHover" id="li_notes">Notes</li>
             <li class="titleHover active" id="li_writeback">Writeback</li>         
            <li class="titleHover" id="li_attorney">Attorney</li>
        </ul>
    </div 

And you can find the active tab using:

 jQuery('#unid').find('li.active').attr('id')
灯下孤影 2024-08-21 11:04:13
var curTab = $jQuery('#tabs .ui-tabs-panel:not(.ui-tabs-hide)').attr('id');
var curTab = $jQuery('#tabs .ui-tabs-panel:not(.ui-tabs-hide)').attr('id');
乙白 2024-08-21 11:04:13

这有效:

$('#divName .ui-tabs-panel[aria-hidden="false"]').prop('id');

This works:

$('#divName .ui-tabs-panel[aria-hidden="false"]').prop('id');
伤痕我心 2024-08-21 11:04:13

对于 jQuery UI >= 1.9,您可以使用 ui.newPanel.selector:

$('#tabs').on('tabactivate', function(event, ui) {
  console.log(ui.newPanel.selector);
});

For jQuery UI >= 1.9 you can use ui.newPanel.selector:

$('#tabs').on('tabactivate', function(event, ui) {
  console.log(ui.newPanel.selector);
});
马蹄踏│碎落叶 2024-08-21 11:04:13

我把头撞在这堵墙上有一段时间了。我有许多引用 url 的选项卡与一堆由 javascript 代码填充的选项卡混合在一起,因此具有在标记中定义的关联 div (.ui-tabs-panel)。

jQuery UI 选项卡小部件使用面板的“aria-labeledby”属性将选项卡与面板链接起来。该属性具有选项卡中包含的锚点的值。因此,对于此标记

<ul><li id='tab_1'><a href='ferndorf.php'>I Be Tab_1</a></li></ul>

附加 jQuery Tab Widget 后,您可以使用找到适当的面板,

panel = $( '.ui-tab-panel[aria-labeledby=' + $( '#tab_1 a' ).attr( 'id' ) + ']' )

反之亦然,您可以使用找到与面板关联的

tab = $( '#' + $( panel ).attr( 'aria-labeledby' ) ).closest( 'li' );

选项卡 选项卡和面板的索引仅在简单结构中相关。

玩得开心。

I knocked my head against this wall for a while. I have a number of tabs that reference urls mixed in with a bunch of tabs that are populated by javascript code and, therefore, have associated divs (.ui-tabs-panel) that are defined in the markup.

jQuery UI Tabs Widget links tabs with panels using the "aria-labeledby" property of the panel. That property has a value of the anchor contained in the tab. So, for this markup

<ul><li id='tab_1'><a href='ferndorf.php'>I Be Tab_1</a></li></ul>

After attaching the jQuery Tab Widget, you can find the appropriate panel using

panel = $( '.ui-tab-panel[aria-labeledby=' + $( '#tab_1 a' ).attr( 'id' ) + ']' )

vice-versa, you can find the tab associated with the panel using

tab = $( '#' + $( panel ).attr( 'aria-labeledby' ) ).closest( 'li' );

The index of tabs and panels is only related in simple structures.

Have fun.

乖乖兔^ω^ 2024-08-21 11:04:13
$("#tabs .ui-state-active a").attr("id");
$("#tabs .ui-state-active a").attr("id");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文