2011-10-11 72 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來跟蹤狀態。 –