我在网页上使用jQuery标签页,但是当页面刷新时,它会丢失我所在的标签页并返回到第一个标签页。
有人遇到这个问题并知道如何解决吗?
我在网页上使用jQuery标签页,但是当页面刷新时,它会丢失我所在的标签页并返回到第一个标签页。
有人遇到这个问题并知道如何解决吗?
和其他人一样,我在jQueryUI 1.10中苦苦挣扎着想要解决我的ui-tabs cookie历史记录问题。多亏了Harry的解决方案以及下面代码中我参考的其他在线文档,现在我有了一个可行的非cookie解决方案!我已经在Firefox 18.0.1和IE 9.0.12中进行了测试。根据我的资源,Chrome、Firefox、Safari和IE8及以上版本都支持会话存储。
$(function() {
// jQueryUI 1.10 and HTML5 ready
// http://jqueryui.com/upgrade-guide/1.10/#removed-cookie-option
// Documentation
// http://api.jqueryui.com/tabs/#option-active
// http://api.jqueryui.com/tabs/#event-activate
// http://balaarjunan.wordpress.com/2010/11/10/html5-session-storage-key-things-to-consider/
//
// Define friendly index name
var index = 'key';
// Define friendly data store name
var dataStore = window.sessionStorage;
// Start magic!
try {
// getter: Fetch previous value
var oldIndex = dataStore.getItem(index);
} catch(e) {
// getter: Always default to first tab in error state
var oldIndex = 0;
}
$('#tabs').tabs({
// The zero-based index of the panel that is active (open)
active : oldIndex,
// Triggered after a tab has been activated
activate : function( event, ui ){
// Get future value
var newIndex = ui.newTab.parent().children().index(ui.newTab);
// Set future value
dataStore.setItem( index, newIndex )
}
});
});
我假设您正在使用jQuery UI选项卡,
以下是使用选项卡+ cookie保存最后点击的选项卡的示例
http://jqueryui.com/demos/tabs/#cookie
演示:打开此链接http://jqueryui.com/demos/tabs/cookie.html,然后关闭它并重新打开,您将看到相同的已点击选项卡
更新: 回答发布3年后,jquery ui已弃用cookie选项:http://jqueryui.com/upgrade-guide/1.9/#deprecated-cookie-option。
但是,如果需要,您仍然可以附加查看此处是否符合您的需求:https://dev59.com/MmYq5IYBdhLWcg3wpyNE#14313315
我刚刚实现的一个更简单的替代方案:
$(".tabs").tabs({
select: function(event, ui) {
window.location.replace(ui.tab.hash);
},
});
那会将哈希值添加到URL中,因此在页面刷新时将重新加载该选项卡,通过使用location.replace
而不是location.hash
,我们不会填充用户历史记录中的不需要的步骤。
希望这有所帮助。既然在jQuery UI 1.10.0中cookie已经被移除,我将Gayathiri的方法与新选项和事件结合使用:
// using cookie plugin from https://github.com/carhartl/jquery-cookie
var tabCookieName = "ui-tabs-1";
$(".tabs").tabs({
active : ($.cookie(tabCookieName) || 0);
activate : function( event, ui ) {
var newIndex = ui.newTab.parent().children().index(ui.newTab);
// my setup requires the custom path, yours may not
$.cookie(tabCookieName, newIndex, { path: window.location.pathname });
}
});
$("#tabs").tabs({
active: localStorage.getItem("currentIdx"),
activate: function(event, ui) {
localStorage.setItem("currentIdx", $(this).tabs('option', 'active'));
}
});
$("#tabs").tabs({
active: localStorage.getItem("currentTabIndex"),
activate: function(event, ui) {
localStorage.setItem("currentTabIndex", ui.newPanel[0].dataset["tabIndex"]);
}
});
示例HTML布局:
<div id="tabs">
<div id="tabs-1" data-tab-index="0">
tab 1 stuff...
</div>
<div id="tabs-2" data-tab-index="1">
tab 2 stuff...
</div>
<div id="tabs-3" data-tab-index="2">
tab 3 stuff...
</div>
</div>
$('a[data-toggle="tab"]').click(function (e) {
e.preventDefault();
$(this).tab('show');
});
$('a[data-toggle="tab"]').on("shown.bs.tab", function (e) {
var id = $(e.target).attr("href");
localStorage.setItem('selectedTab', id)
});
var selectedTab = localStorage.getItem('selectedTab');
if (selectedTab != null) {
$('a[data-toggle="tab"][href="' + selectedTab + '"]').tab('show');
}
对我来说总是很好用。
包含jquery cookies插件:
<script type="text/javascript" src="/resources/js/jquery.cookies.2.2.0.min.js"></script>
在$.function({..或document.ready中声明一个cookie名称
var cookieName = "mycookie";
$("#tabs").tabs({
selected : ($.cookies.get(cookieName) || 0),
select : function(e, ui) {
$.cookies.set(cookieName, ui.index);
}
});
# Set selected_tab to the correct index based on the current url anchor hash
selected_tab = 0
if matches = /#(\w+)/.exec(window.location.hash)
# find the index of the tab with the given id
selected_tab = $('#' + matches[1]).index() - 1
# Initialize the tabs and set 'selected' to equal the selected_tab variable we just set
$('.tabable').tabs
selected: selected_tab, # this is where the magic happens
select: (evt, ui) ->
window.location.hash = ui.panel.id # set an anchor tag in the url with the tab id