jQuery UI 选项卡 - 如何获取当前选定的选项卡索引
我知道这个具体问题已经之前被问过,但我没有得到任何使用 jQuery UI Tabs
插件上的 bind()
事件得到的结果。
我只需要新选择的选项卡的索引即可在单击该选项卡时执行操作。 bind()
允许我挂钩 select 事件,但我获取当前选定选项卡的常用方法不起作用。 它返回之前选择的选项卡索引,而不是新的选项卡索引:
var selectedTab = $("#TabList").tabs().data("selected.tabs");
这是我尝试用来获取当前选择的选项卡的代码:
$("#TabList").bind("tabsselect", function(event, ui) {
});
当我使用此代码时,ui 对象返回未定义
。 从文档来看,这应该是我用来使用 ui.tab 挂钩到新选择的索引的对象。 我已经在初始 tabs()
调用中尝试过此操作,并且也单独尝试过此操作。 我在这里做错了什么吗?
I know this specific question has been asked before, but I am not getting any results using the bind()
event on the jQuery UI Tabs
plugin.
I just need the index
of the newly selected tab to perform an action when the tab is clicked. bind()
allows me to hook into the select event, but my usual method of getting the currently selected tab does not work. It returns the previously selected tab index, not the new one:
var selectedTab = $("#TabList").tabs().data("selected.tabs");
Here is the code I am attempting to use to get the currently selected tab:
$("#TabList").bind("tabsselect", function(event, ui) {
});
When I use this code, the ui object comes back undefined
. From the documentation, this should be the object I'm using to hook into the newly selected index using ui.tab. I have tried this on the initial tabs()
call and also on its own. Am I doing something wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
获取所选选项卡索引的另一种方法是:
Another way to get the selected tab index is:
在 url 变量中,您将获取当前选项卡的 HREF / URL
In the url variable you will get the current tab's HREF / URL
采用像
''
这样的隐藏变量,并在每个选项卡的 onclick 事件上编写像 .. 。您可以在发布的页面上获取“sel_tab”的值 :) , 简单的 !!!
take a hidden variable like
'<input type="hidden" id="sel_tab" name="sel_tab" value="" />'
and on each tab's onclick event write code like ...you can get the value of 'sel_tab' on posted page. :) , simple !!!
如果您想确保
ui.newTab.index()
在所有情况下(本地和远程选项卡)都可用,请在 activate 函数中将其调用为 文档 说:http://jsfiddle.net/7v7n0v3j/
If you want to ensure
ui.newTab.index()
is available in all situations (local and remote tabs), then call it in the activate function as the documentation says:http://jsfiddle.net/7v7n0v3j/
如果您需要从选项卡事件上下文之外获取选项卡索引,请使用以下命令:
更新:
从版本 1.9 开始,“已选择”更改为“活动”
If you need to get the tab index from outside the context of a tabs event, use this:
Update:
From version 1.9 'selected' is changed to 'active'
对于 1.9 之前的 JQuery UI 版本:
event
中的ui.index
就是您想要的。对于 JQuery UI 1.9 或更高版本:请参阅下面 Giorgio Luparia 的答案。
For JQuery UI versions before 1.9:
ui.index
from theevent
is what you want.For JQuery UI 1.9 or later: see the answer by Giorgio Luparia, below.
如果您使用的是 JQuery UI 版本 1.9.0 或更高版本,则可以在函数内访问 ui.newTab.index() 并获取所需内容。
对于早期版本,请使用 ui.index。
If you're using JQuery UI version 1.9.0 or above, you can access ui.newTab.index() inside your function and get what you need.
For earlier versions use ui.index.
¡¡¡ 更新! 请注意:在较新版本的 jQueryUI (1.9+) 中,
ui-tabs-selected
已替换为ui-tabs-active
。 !!!我知道这个线程很旧,但是我没有看到提到的是如何从“选定的选项卡”(当前下拉面板)之外的其他地方获取“选定的选项卡”(当前下拉面板)选项卡事件”。我确实有一个简单的方法......
为了轻松获取索引,当然有网站上列出的方法......
但是,您可以使用我的第一种方法来获取索引以及您想要的有关该面板的任何内容非常简单...
PS。 如果您使用 iframe 变量,然后使用 .find('.ui-tabs-panel:not(.ui-tabs-hide)'),您会发现对框架中的选定选项卡执行此操作也很容易。
请记住,jQuery 已经完成了所有艰苦的工作,无需重新发明轮子!
有人向我提出了一个问题:“如果视图上有多个选项卡区域怎么办?”
再次强调,只需简单思考,使用与我相同的设置,但使用 ID 来标识您想要获取哪些选项卡。
例如,如果您有:
并且您想要第二个选项卡集的当前面板:
并且如果您想要实际选项卡而不是面板(非常简单,这就是为什么我之前没有提到它,但我想我现在会,只是为了彻底)
再次记住,jQuery 完成了所有艰苦的工作,不要想得那么辛苦。
¡¡¡ Update! Please note: In newer versions of jQueryUI (1.9+),
ui-tabs-selected
has been replaced withui-tabs-active
. !!!I know this thread is old, butsomething I didn't see mentioned was how to get the "selected tab" (Currently dropped down panel) from somewhere other than the "tab events".I do have a simply way ...
And to easily get the index, of course there is the way listed on the site ...
However, you could use my first method to get the index and anything you want about that panel pretty easy ...
PS. If you use an iframe variable then .find('.ui-tabs-panel:not(.ui-tabs-hide)'), you will find it easy to do this for selected tabs in frames as well.
Remember, jQuery already did all the hard work, no need to reinvent the wheel!
Question was brought up to me, "What if there are more than one tabs areas on the view?"
Again, just think simple, use my same setup but use an ID to identify which tabs you want to get hold of.
For example, if you have:
And you want the current panel of the second tab set:
And if you want the ACTUAL tab and not the panel (really easy, which is why I ddn't mention it before but I suppose I will now, just to be thorough)
Again, remember, jQuery did all the hard work, don't think so hard.
你什么时候尝试访问 ui 对象? 如果您尝试在绑定事件之外访问 ui 将是未定义的。
另外,如果
在这样的事件中运行此行:
selectedTab 将等于该时间点的当前选项卡(“上一个”选项卡)。这是因为“tabsselect”事件在单击的选项卡成为当前选项卡之前被调用。 如果您仍然想这样做,请使用“tabsshow”代替,将导致 selectedTab 等于单击的选项卡。
但是,如果您想要的只是索引,那么这似乎过于复杂。 事件内的 ui.index 或事件外的 $("#TabList").tabs().data("selected.tabs") 应该就是您所需要的。
When are you trying to access the ui object? ui will be undefined if you try to access it outside of the bind event.
Also, if this line
is ran in the event like this:
selectedTab will equal the current tab at that point in time (the "previous" one.) This is because the "tabsselect" event is called before the clicked tab becomes the current tab. If you still want to do it this way, using "tabsshow" instead will result in selectedTab equaling the clicked tab.
However, that seems over-complex if all you want is the index. ui.index from within the event or $("#TabList").tabs().data("selected.tabs") outside of the event should be all that you need.
这在 1.9 版本中发生了变化,
类似的东西
应该使用 。 这对我来说效果很好;-)
this changed with version 1.9
something like
should be used. This is working fine for me ;-)
如果有人尝试从 iframe 内访问选项卡,您可能会发现这是不可能的。 选项卡的
div
永远不会被标记为已选择,就像隐藏或不隐藏一样。 链接本身是唯一标记为选定的部分。以下内容将为您提供链接的
href
值,该值应与选项卡容器的 id 相同:这也应该可以代替:
$tabs.tabs('option' , 'selected');
从某种意义上说,它不是仅仅获取选项卡的索引,而是为您提供选项卡的实际 id。
In case anybody has tried to access tabs from within an iframe, you may notice it's not possible. The
div
of the tab never gets marked as selected, just as hidden or not hidden. The link itself is the only piece marked as selected.The following will get you the
href
value of the link which should be the same as the id for your tab container:This should also work in place of:
$tabs.tabs('option', 'selected');
It's better in the sense that instead of just getting the index of the tab, it gives you the actual id of the tab.
最简单的方法是
和索引
the easiest way of doing this is
and for index
我发现下面的代码可以解决问题。 设置新选择的选项卡索引的变量
I found the code below does the trick. Sets a variable of the newly selected tab index
如果您找到活动选项卡索引,然后指向活动选项卡,
首先获取活动索引,
然后使用 css 类获取选项卡内容面板,
现在将其包装在 jQuery 对象中以进一步在
此处使用它,我想在类中添加两个信息
.ui-tabs-nav
用于与选项卡内容面板关联的导航,.ui-tabs-panel
与选项卡内容面板关联。 在 jquery ui 网站的这个链接演示中,您将看到使用了该类 - http://jqueryui.com/tabs/ #操纵In case if you find Active tab Index and then point to Active Tab
First get the Active index
Then using the css class get the tab content panel
now wrapped it in jQuery object to further use it
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 开始的简单
then you will have the index of tab from 0
simple
您可以在下一篇文章中发布以下答案
You can post below answer in your next post
请尝试以下操作:
Try the following:
您可以通过以下方式找到它:
You can find it via: