在jquery菜单中保留状态

在jquery菜单中保留状态,jquery,menu,Jquery,Menu,我有一个jquery菜单,单击它可以显示特定的div元素。 我想知道我是否在div3处于活动状态(可见)时刷新页面,如何在重新加载页面后使div3再次处于活动状态 谢谢使用cookie跟踪状态如何?我通常在URL中使用#哈希标记和哈希轮询来实现这一点 以下是一个例子: <a href="#showdiv">Click here to show div!</a> var start_hash = window.location.hash; var recent_hash

我有一个jquery菜单,单击它可以显示特定的div元素。 我想知道我是否在div3处于活动状态(可见)时刷新页面,如何在重新加载页面后使div3再次处于活动状态


谢谢

使用cookie跟踪状态如何?

我通常在URL中使用#哈希标记和哈希轮询来实现这一点

以下是一个例子:

<a href="#showdiv">Click here to show div!</a>

var start_hash = window.location.hash;
var recent_hash = ""; 

process_hash(start_hash);  // Start the initial hash check
setInterval(poll_hash, 100);    // Initialize our hash polling interval

function poll_hash() {
 if (window.location.hash==recent_hash) { return; } // No change in URL hash
 recent_hash = window.location.hash;            // Set to check next time. 
 process_hash(recent_hash);             // Changed hash, lets process
}

// process_hash    
function process_hash(current_hash) {
 // Check for defaults, then set to #home.
 if(!current_hash || current_hash == '') { current_hash="#home"; }

 // Strip the # for the hash
 var hash_id = link_div.match(/[^#]+/g);                

 // conditions for multiple hash tags can go here. 
 if(hash_id == "#showdiv") {
  $("#somediv").show();
 }
}

var start_hash=window.location.hash;
var最近_hash=“”;
进程_散列(开始_散列);//开始初始哈希检查
setInterval(poll_hash,100);//初始化哈希轮询间隔
函数poll_hash(){
if(window.location.hash==recent_hash){return;}//URL哈希没有变化
最近的\u hash=window.location.hash;//设置为下次检查。
process_hash(最近的_hash);//已更改的hash,让我们处理
}
//进程哈希
函数进程\u散列(当前\u散列){
//检查默认值,然后设置为#home。
如果(!current_hash | | current_hash==''){current_hash=“#home”}
//把#去掉做散列
var hash_id=link_div.match(/[^#]+/g);
//多个散列标记的条件可以在这里找到。
if(hash_id==“#showdiv”){
$(“#somediv”).show();
}
}

当然可以通过url.com/page.php#showdiv访问此文件以恢复状态。如果您有多个元素,或者根据您是否不希望在客户端之间保存状态,您可能希望使用cookie来跟踪状态。我下载了jQuery cookie插件,它现在正在工作。谢谢你的帮助。