jQuery选项卡,从内部链接到另一个选项卡 [英] jQuery tabs, link to another tab from inside
问题描述
我有 jQuery ui标签页,我希望能够从链接中打开标签页(在当前标签页或页面的内部或外部)
I have jQuery ui tabs and I would like to be able to open a tab from a link (inside or outside the current tab or page)
如果链接在外部页面中,并且看起来像这样,它将起作用:
It does work if the link is in an external page and looks like this:
http://server.com/view.php?id=130#section-2
这样可以正确打开选项卡"section-2"
This correctly open the tab "section-2"
问题是,当链接位于同一页面中时,它不会刷新页面(因为它只是添加#section-2),因此未选中该选项卡.
The problem is when the link is in the same page, it does not refresh the page (as it just add the #section-2) so the tab is not selected.
我看到了一些在标记中使用id并绑定jQuery中的单击的答案示例,但是我无法使用它,因为我的某些链接是经过计算的,并且我不知道它们是指向内部还是外部.
I saw some answers example using ids in the tag and binding the click in jQuery, but I cannot use this because some of my links are computed and I don't know if they are pointing to inside or outside.
推荐答案
在链接到标签的标签上添加ID"section-2-link"(在末尾添加"-link",以避免与ID相同)标签内容).然后将其添加到js
Add the id "section-2-link" to the tags linked to the tabs (add the '-link' at the end to avoid having same id as the tab content). And then add this to js
jQuery(window).bind('hashchange', function () { //detect hash change
var hash = window.location.hash.slice(1); //hash to string (= "section-2")
jQuery('#' + hash + '-link').trigger("click");
});
这可以检测url上的哈希值何时更改,并触发对所需链接的点击(打开正确的标签)
This detects when the hash on url changes and trigger the click on the link you need (to open the correct tab)
这篇关于jQuery选项卡,从内部链接到另一个选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!