【发布时间】:2011-10-11 19:27:10
【问题描述】:
我有 jquery 菜单,点击时会显示特定的 div 元素。 我想知道当例如 div3 处于活动状态(可见)时是否刷新页面,重新加载页面后如何使 div3 再次处于活动状态?
谢谢
【问题讨论】:
我有 jquery 菜单,点击时会显示特定的 div 元素。 我想知道当例如 div3 处于活动状态(可见)时是否刷新页面,重新加载页面后如何使 div3 再次处于活动状态?
谢谢
【问题讨论】:
使用 cookie 来跟踪状态怎么样?
【讨论】:
我通常在 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();
}
}
【讨论】: