我一直在尝试找到一种方法,在Jquery UI Tabs中将window.location.hash更改为当前选定的选项卡。
我试过了:
$("#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有关,干扰了它。接受的答案和另一个稍微修改过的建议答案都可以工作。谢谢大家。
发布于 2009-02-20 17:16:28
在您的事件处理函数中,ui.tab
是一个锚元素。您可以像这样检索它的哈希值:
$("#tabs > ul").tabs();
$("#tabs > ul").bind("tabsshow", function(event, ui) {
window.location.hash = ui.tab.hash;
})
这对你有效吗?
发布于 2010-10-19 21:46:45
$('#tabs').tabs({
select: function(event, ui) {
window.location.hash = ui.tab.hash;
}
});
发布于 2012-10-06 01:44:18
虽然上面的一些解决方案有效,但它们在某些情况下会导致页面跳转,因为window.location.hash应用编程接口是为将您带到页面的特定部分而设计的。
这个整洁的解决方案提出了here,解决了这个问题
$("#tabs").bind("tabsshow", function(event, ui) {
history.pushState(null, null, ui.tab.hash);
});
https://stackoverflow.com/questions/570276
复制相似问题