在 Jquery-UI 选项卡中按名称切换到选定的选项卡
如果我有三个选项卡:
<div id="tabs">
<ul>
<li><a href="#sample-tab-1"><span>One</span></a></li>
<li><a href="#sample-tab-2"><span>Two</span></a></li>
<li><a href="#sample-tab-3"><span>Three</span></a></li>
</ul>
</div>
我想按其名称交换到#sample-tab-2。 我知道如果我知道它的数字,我可以切换到选项卡,但在我遇到的情况下我不会知道。
注:JQuery 1.3.1 / JQuery-UI 1.6rc6
If I have three tabs:
<div id="tabs">
<ul>
<li><a href="#sample-tab-1"><span>One</span></a></li>
<li><a href="#sample-tab-2"><span>Two</span></a></li>
<li><a href="#sample-tab-3"><span>Three</span></a></li>
</ul>
</div>
I would like to swap to #sample-tab-2 by it's name. I know I can switch to a tab if I know it's number, but in the case I've run into I won't know that.
Notes: JQuery 1.3.1 / JQuery-UI 1.6rc6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
我无法让之前的答案发挥作用。 我执行了以下操作来按名称获取选项卡的索引:
I could not get the previous answer to work. I did the following to get the index of the tab by name:
看来使用 id 和索引一样有效,
例如,只需这样做即可开箱即用...
这在官方文档中有详细记录:
我认为这是在提出这个问题之后并且可能在大多数答案之后添加的
It seems that using the id works as well as the index,
e.g. simply doing this will work out of the box...
This is well documented in the official docs:
I assume this was added after this question was asked and probably after most of the answers
jquery ui 1.10.0 不支持“select”方法。http://api.jqueryui.com/ tabs/
我使用这段代码,并且工作正常:
Methods `'select' isn't support in jquery ui 1.10.0.http://api.jqueryui.com/tabs/
I use this code , and work correctly:
您可以使用以下脚本按名称获取选项卡的索引:
You can get the index of the tab by name with the following script:
只有这段代码真正有效!
Only this code real works!
使用此功能:
并使用以下代码在选项卡之间切换:
Use this function:
And use following code to switch between tabs:
试试这个:“选择”/“活动”选项卡
// `'select' 在 jquery ui 版本 1.10.0 中不支持
替代解决方案:使用“active”:
try this: "select" / "active" tab
// `'select' does not support in jquery ui version 1.10.0
alternate solution: use "active":
获取选项卡从零开始的索引的唯一实用方法是逐步遍历构成选项卡集的每个元素(LI>A)并匹配其内部文本。 它可能可以以更干净的方式完成,但我是这样做的。
你可以看到我使用了一个隐藏的 页面中的字段,以确保变量从一个函数移动到另一个函数。
注意:有一点问题——激活选项卡集后选择选项卡似乎并不像 jQuery UI 1.8 中宣传的那样工作,这就是为什么我使用第一遍中识别的索引来初始化选项卡集选择所需的选项卡。
The only practical way to get the zero-based index of your tabs is to step through each of the elements that make the tabset (the LI>A s) and match on their inner text. It can probably be done in a cleaner way, but here's how I did it.
You can see that I used a hidden <input id="reqTab"> field in the page to make sure the variable moved from one function to the other.
NOTE: There is a little bit of a gotcha -- selecting tabs after the tabset is activated doesn't seem to work as advertised in jQuery UI 1.8, which is why I used the identified index from my first pass in order to initialize the tabset with the desired tab selected.
以下内容对我有用
$($("#tabs")[0]).tabs({selected: 1});
希望,这有帮助!
The following piece worked for me
$($("#tabs")[0]).tabs({selected: 1});
Hope, this helps!
如果要更改 href,可以为链接分配一个 id
One
这样你就可以通过它的 id 找到选项卡索引。If you are changing the hrefs, you can assign an id to the links
<a href="#sample-tab-1" id="tab1"><span>One</span></a>
so you can find the tab index by it's id.@bduke 的答案实际上只需稍加调整即可。
上面的假设类似于:
jQueryUI 现在支持使用选项卡的 ID/HREF 选择器调用“select”,但是在构造选项卡时,“selected”选项仍然只支持数字索引。
我投票给 bdukes,因为他们让我走上了正确的道路。 谢谢!
@bduke's answer actually works with a slight tweak.
Above assumes something similar to:
jQueryUI now supports calling "select" using the tab's ID/HREF selector, but when constructing the tabs, the "selected" Option still only supports the numeric index.
My vote goes to bdukes for getting me on the right track. Thanks!
以下是按名称获取所选选项卡的示例代码。 我希望这可以帮助您找到解决方案:
Here is a sample code to get the selected tab by name. I hope this aids you to find ypur solution:
或者,您可以为链接分配一个 id,
以便您可以找到它的 id。
or, you can assign an id to the links
so you can find it's id.
将特定选项卡索引设置为活动状态:
将最后一个选项卡设置为活动状态
按 ID 设置特定选项卡:
Set specific tab index as active:
Set last tab as active
Set specific tab by ID:
我很难得到任何答案,因为它们基于旧版本的 JQuery UI。 我们使用的是 1.11.4 (CDN 参考)。
这是我的小提琴工作代码: http://jsfiddle.net/6b0p02um/
我最终将来自四个或五个不同线程的位拼接在一起以使我的线程工作:
I had trouble getting any of the answers to work as they were based on the older versions of JQuery UI. We're using 1.11.4 (CDN Reference).
Here is my Fiddle with working code: http://jsfiddle.net/6b0p02um/
I ended up splicing together bits from four or five different threads to get mine to work:
这适用于 Bootstrap v5 选项卡:
选择选项卡 id:tab-2
This works for Bootstrap v5 Tabs:
To select tab id: tab-2