【问题标题】:Preserve state in jquery menu在 jquery 菜单中保留状态
【发布时间】:2011-10-11 19:27:10
【问题描述】:

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

谢谢

【问题讨论】:

    标签: jquery menu


    【解决方案1】:

    使用 cookie 来跟踪状态怎么样?

    【讨论】:

    • 我下载了 jQuery cookie 插件,现在可以使用了。感谢您的帮助。
    【解决方案2】:

    我通常在 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();
     }
    }
    

    【讨论】:

    • 这当然可以通过 url.com/page.php#showdiv 访问以恢复状态。如果您有多个元素,或者根据您是否不希望在客户端之间保存状态,您可能需要使用 cookie 来跟踪状态。
    猜你喜欢
    • 1970-01-01
    • 2016-08-27
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多