嵌套 jQuery UI 选项卡后退按钮历史记录
我正在使用 jQuery Address 插件 使我的 jQuery UI 选项卡可添加书签并兼容返回按钮历史记录。效果很好。
但是,我想知道是否可以为嵌套选项卡提供相同类型的“深度链接”?例如,我有以下内容:
<div id="tabs">
<ul class="links">
<li><a href="#one">main tab one</a></li>
<li><a href="#two">main tab two</a></li>
<li><a href="#three">main tab three</a></li>
<li><a href="#four">main tab four</a></li>
</ul>
<div id="one"> Main tab one content </div>
<div id="two">
Main tab two content
<div id="nested">
<ul>
<li><a href="#nestedone">nested tab one</a></li>
<li><a href="#nestedtwo">nested tab two</a></li>
<li><a href="#nestedthree">nested tab three</a></li>
</ul>
<div id="nestedone"> nested tab one </div>
<div id="nestedtwo"> nested tab two </div>
<div id="nestedthree"> nested tab three </div>
</div> <!-- end nested -->
</div>
<div id="three"> Main tab three content </div>
<div id="fout"> Main tab four content </div>
</div> <!-- end tabs -->
<script>
$(document).ready(function(){
/* main tabs stuff */
var $tabs = $("#tabs").tabs().addClass('ui-tabs-vertical ui-helper-clearfix');
$("#tabs li").removeClass('ui-corner-top').addClass('ui-corner-left');
/* nested tabs */
var $nested = $("#nested").tabs().addClass('ui-tabs-hz');
$("#nested ul").removeClass('ui-tabs-nav');
/* show the hash's tab whenever the address is changed */
$.address.change(function(event){
$("#tabs").tabs( "select" , window.location.hash ) ;
})
/* when the tab is selected update the url with the hash */
$("#tabs").bind("tabsselect", function(event, ui) {
window.location.hash = ui.tab.hash;
})
});
</script>
一切都适用于主(外部选项卡):
- 选项卡的 href 链接被放在 URL 上,因为 a 具有正确的
- 外部选项卡遵守浏览器的后退和前进按钮
- 外部选项卡可添加书签
但是,我可以内部选项卡似乎没有获得相同类型的功能。
鉴于嵌套选项卡包含在“主”选项卡之一中,上面的绑定函数将 $(this) 视为外部选项卡(即使单击嵌套选项卡时)。结果是 JavaScript 代码对嵌套选项卡正确执行,但随后执行时就像最后一个主(外部)选项卡是所选选项卡一样。
我似乎无法弄清楚如何在处理嵌套选项卡之后和运行最后一个主(外部)选项卡之前停止执行 JavaScript。我能得到的最接近的结果是将以下内容添加到绑定函数中,并停止在包含嵌套选项卡的外部选项卡上执行。
$("#tabs").bind("tabsselect", function(event, ui) {
if ($(ui.tab).closest('div').attr('id') !== "nested") {
window.location.hash = ui.tab.hash;
}
})
我确信我错过了一些明显的东西。有什么建议吗?
谢谢!
I'm using the jQuery Address plug-in to make my jQuery UI tabs bookmarkable and compliant to back button history. Works great.
However, I was wondering if it was possible to do provide the same sort of 'deep linking' for nested tabs? E.g., I have the following:
<div id="tabs">
<ul class="links">
<li><a href="#one">main tab one</a></li>
<li><a href="#two">main tab two</a></li>
<li><a href="#three">main tab three</a></li>
<li><a href="#four">main tab four</a></li>
</ul>
<div id="one"> Main tab one content </div>
<div id="two">
Main tab two content
<div id="nested">
<ul>
<li><a href="#nestedone">nested tab one</a></li>
<li><a href="#nestedtwo">nested tab two</a></li>
<li><a href="#nestedthree">nested tab three</a></li>
</ul>
<div id="nestedone"> nested tab one </div>
<div id="nestedtwo"> nested tab two </div>
<div id="nestedthree"> nested tab three </div>
</div> <!-- end nested -->
</div>
<div id="three"> Main tab three content </div>
<div id="fout"> Main tab four content </div>
</div> <!-- end tabs -->
<script>
$(document).ready(function(){
/* main tabs stuff */
var $tabs = $("#tabs").tabs().addClass('ui-tabs-vertical ui-helper-clearfix');
$("#tabs li").removeClass('ui-corner-top').addClass('ui-corner-left');
/* nested tabs */
var $nested = $("#nested").tabs().addClass('ui-tabs-hz');
$("#nested ul").removeClass('ui-tabs-nav');
/* show the hash's tab whenever the address is changed */
$.address.change(function(event){
$("#tabs").tabs( "select" , window.location.hash ) ;
})
/* when the tab is selected update the url with the hash */
$("#tabs").bind("tabsselect", function(event, ui) {
window.location.hash = ui.tab.hash;
})
});
</script>
Everything works perfectly for the main (outer tabs):
- the tab's href link gets put on the URL as a has correctly
- outer tabs obey the browser's back and forward buttons
- outer tabs are bookmark-able
However, I can't seem to get the same type of functionality for the inner tabs.
Given the nested tabs are contained in one of the 'main' tabs, the above bind function above sees $(this) as being the outer tab (even when an nested tab is clicked). The result is the JavaScript code executes correctly for the nested tab, but then executes as if the last main (outer) tab was the tab selected.
I can't seem to figure out how to stop the JavaScript from executing after the nested tab has been processed and before the last main (outer) tab is run. The closest I can get is the adding the following to the bind function and stop the execution on the outer tab containing the nested tabs.
$("#tabs").bind("tabsselect", function(event, ui) {
if ($(ui.tab).closest('div').attr('id') !== "nested") {
window.location.hash = ui.tab.hash;
}
})
I'm sure I'm missing something obvious. Any suggestions?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要采取某种解决方法来组合哈希值。我对 .address 插件没有太多经验,但是像这样的东西怎么样:
对于 javascript:
You'd probably have to do some sort of workaround to combine the hashes. I don't have much experience with the .address plugin, but how about something like this:
And for the javascript: