2011-10-11 74 views
1

我有jquery菜单,点击时显示特定的div元素。 我不知道我刷新页面时,例如div3是活动的(可见),我怎样才能使div3页面重新加载后再次活动?在jquery菜单中保留状态

谢谢

回答

2

如何使用Cookie来跟踪状态?

+0

我下载的jQuery插件饼干,现在正在工作。感谢您的帮助。 – 22db05

+0

@Dusko - 很高兴能帮到你! – Sam

0

我通常在URL和散列轮询中使用#hash标签来实现这一点。

下面是一个例子:

<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(); 
} 
} 
+0

这当然是通过url.com/page.php#showdiv来恢复状态。如果您有多个元素,或者取决于您是否希望状态在客户端之间保存,则可能需要使用Cookie来跟踪状态。 –