需要 jQuery 选项卡当前选定的选项卡 ID
我知道我可以获取当前所选选项卡的索引,但是我可以以某种方式获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您可以使用
:visible
伪选择器 来定位可见面板:值得注意的是,您可以从 activate 检索活动选项卡事件:
You can use the
:visible
pseudo-selector to target the visible panel:It's worth noting that you can retrieve the active tab from the activate event:
乔纳森·桑普森的答案不再有效。尝试...
$("#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/
如果您想要从所选选项卡中获取
id
(或者实际上是href
),您可以使用eq()
来检索 jQuery 对象。您可以在此处查看示例:http://jsfiddle.net/svirkant/hpU3T/1/
If you want the
id
(or actually thehref
) from the selected tab, you can useeq()
to retrieve the jQuery Object.You can see an example here: http://jsfiddle.net/svierkant/hpU3T/1/
正如我在这个问题的回答中发布的那样,有多种方法可以实现这一目标。
在 jQuery 文档上,他们建议执行以下操作来查找索引当前打开的选项卡:
但是,如果您需要使用该选项卡执行大量操作,则这是不切实际的。
为什么他们还没有提供获取实际元素的更实用的解决方案,我不确定,但是,通过使用 jQuery,您可以自己创建一个简单的解决方案。
在下面的代码中,我将向您展示使用当前选项卡执行您想要的任何操作是多么容易:
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:
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:
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" );
对于 1.9 以下的 jquery 版本:
您可以使用以下命令找到活动选项卡:
For jquery version below 1.9:
And you can find the active tab using:
这有效:
This works:
对于 jQuery UI >= 1.9,您可以使用 ui.newPanel.selector:
For jQuery UI >= 1.9 you can use
ui.newPanel.selector
:我把头撞在这堵墙上有一段时间了。我有许多引用 url 的选项卡与一堆由 javascript 代码填充的选项卡混合在一起,因此具有在标记中定义的关联 div (.ui-tabs-panel)。
jQuery UI 选项卡小部件使用面板的“aria-labeledby”属性将选项卡与面板链接起来。该属性具有选项卡中包含的锚点的值。因此,对于此标记
附加 jQuery Tab Widget 后,您可以使用找到适当的面板,
反之亦然,您可以使用找到与面板关联的
选项卡 选项卡和面板的索引仅在简单结构中相关。
玩得开心。
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
After attaching the jQuery Tab Widget, you can find the appropriate panel using
vice-versa, you can find the tab associated with the panel using
The index of tabs and panels is only related in simple structures.
Have fun.