使用 jquery ui 选项卡更改 location.hash
我一直在尝试找到一种方法将 window.location.hash 更改为 Jquery UI 选项卡。
我已经尝试过:
$("#tabs > ul").tabs();
$("#tabs > ul").bind("tabsshow", function(event, ui) {
window.location.hash = ui.tab;
})
这会导致在更改选项卡时将哈希更改为#undefined。
我也尝试过:
$("#tabs > ul").tabs({
select: function(event, ui) {
window.location.hash = ui.tab }
});
但这似乎根本没有被触发。
任何帮助,将不胜感激。 谢谢。
编辑:看起来我最初的问题的一部分与其他地方的 js 干扰有关。 已接受的答案和稍作修改的其他建议答案都有效。 谢谢大家。
I've been trying to find a way to change the window.location.hash to the currently selected tab in Jquery UI Tabs.
I've tried:
$("#tabs > ul").tabs();
$("#tabs > ul").bind("tabsshow", function(event, ui) {
window.location.hash = ui.tab;
})
This results in changing the hash to #undefined when the tab is changed.
I've also tried:
$("#tabs > ul").tabs({
select: function(event, ui) {
window.location.hash = ui.tab }
});
But this doesn't seem to be triggered at all.
Any help would be appreciated. Thanks.
Edit: It looks like part of my initial problem had something to do with js somewhere else interfering with this. Both the accepted answer and the other suggested answer slightly modified do work. Thanks all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
在您的事件处理函数中,
ui.tab
是一个锚元素。 您可以像这样检索其哈希值:这对您有用吗?
In your event handler function
ui.tab
is an anchor element. You can retrieve its hash value like this:Does this work for you?
虽然上述某些解决方案有效,但在某些情况下它们会导致页面跳转,因为 window.location.hash API 旨在将您带到页面的特定部分。
此处提出的这个简洁的解决方案解决了这个问题问题
While some of the above solutions work, they cause the page to jump around in some cases, as the window.location.hash API is designed to bring you to a specific part of the page.
This neat solution proposed here, solves that issue
这对我有用,jQuery UI 1.10:
另请参阅:http://api.jqueryui.com/tabs/#event-激活
This worked for me, jQuery UI 1.10:
See also: http://api.jqueryui.com/tabs/#event-activate
我的简单解决方案不跳转:
my simple solution without jumping:
这就是你想要的吗?
Would this be what you're going for?
上面的许多答案不适用于当前版本的 jQuery UI 选项卡。 这是我目前正在使用的:
Many of the above answers don't work with the current version of jQuery UI Tabs. Here's what I'm currently using:
我的 jQuery UI 1.10.3 方法
My way for jQuery UI 1.10.3
这对我使用 Jquery 1.8 有用
This worked for me using Jquery 1.8
在查看了其他一些问题和 jQueryUI API 文档之后,我发现这最终对我有用。
After looking through some other questions and the jQueryUI API docs I found that this ended up working for me.
我正在使用一个选项卡插件,你可以在 github 上找到它: https://github.com/jquerytools /jquerytools.github.com
I'm using a tab plugin you can find it at github: https://github.com/jquerytools/jquerytools.github.com
看起来 ui.tab 本身没有返回有效的字符串。 (相反,它返回未定义,所以你会说它根本不返回任何内容。)
尝试在 ui.jquery.js 的开发版本中查看是否有任何返回,也许你必须调用 ui 的子级。标签 ;-)
It seems like ui.tab itself doesn't return a valid string. (instead it returns undefined, so you'd say it doesn't return anything at all.)
Try looking around in the dev version of ui.jquery.js whether there are any returns there, maybe you have to call a child of ui.tab ;-)
这段代码对我来说工作正常:
This code works ok for me:
这段代码对我有用:
This code works for me :
以下代码对我有用..
The following code is worked for me..
我的工作解决方案。
jQuery v3.5.1、jQuery UI v1.12.1
My working solution.
jQuery v3.5.1, jQuery UI v1.12.1
这段代码对我有用:
This piece of code worked for me: