2011-02-15 30 views

回答

1

jquery cookie plugin可以简化cookie管理。就显示/隐藏HTML而言,您可以查看show()hide()方法。

+0

我只使用这个cookie插件,它工作的一种享受! – 2011-02-15 21:58:38

0

这真的取决于事件/原因的内容需要显示/隐藏...

是必须出现在某个特定的用户,如果是的话,你是如何识别用户(会话吧的具体内容,openID)? 还是事件驱动,即用户点击按钮和内容显示/隐藏和cookie存储显示/隐藏状态?

  • 达摩
+0

用户点击一个按钮和内容显示/隐藏和cookie存储显示/隐藏状态?我需要隐藏类:.panel或带有类的div:#panel images:http://sis.windhover.com/buy/images/btn_show.gif和http://companies.elsevierbi.com/内容/图片/ btn_hide.gif – user616324 2011-02-16 09:36:17

0

比你需要的可能更多,但我用这个用tablesorter插件以折叠/展开表的部分,存储在cookie中,并与.toggle状态(),你可以得到一个很好的效果。

function tableContainer(id,visible,sortColumn,sortOrder){ 
    this.ID = id; 
    this.Visible = visible; 
    this.SortColumn = sortColumn; 
    this.SortOrder = sortOrder; 
} 

function bindTableHeaders(element){ 
    //Bind click handler to the table THs to update object as to current sort column. 
    $("thead th","#" + element).bind("click",function(){ 
    var order = this.order 
    var column = this.column 
    var $table = $(this).closest("table") 
    var visible = $table.attr("expanded") //Technically I suppose if you can click these then it must be visible 
    var id = $table.attr("id") 

    var tableObj = new tableContainer(id,visible,column,order); 

    $.cookie(element, JSON.stringify(tableObj), { secure: true }); //Write the current state into the section cookie 
    }); 
}; 

function recoverState(element) { 
    // pull cookie for page state and visibility 
    var elementData = $.cookie(element); 

    if (elementData != null){ 
    // parse JSON based on object representation 
    var json = JSON.parse(elementData) 
    var id = json.ID; 
    var visible = json.Visible; 
    var sortColumn = json.SortColumn == undefined ? 0 : json.SortColumn 
    var sortOrder = json.SortOrder == undefined ? 0 : json.SortOrder 
    } else { 
    var id = element; 
    var visible = "true" 
    var sortColumn = 0; 
    var sortOrder = 0; 
    } 

    // switch visibility 
    if(visible == "false"){ 
    toggleElement(element) 
    } 

    // Determine if this section has any data (eg. a <tbody>) 
    if ($("tbody","#" + id).length == 0 || $("tbody","#" + id).html() == "") 
    return 

    if (pageAction == "Edit"){ 
     $("#" + id).tablesorter({widgets: ['zebra'], sortList: [[sortColumn,sortOrder]]});  
    } else { 
     $("#" + id) 
     .collapsible("td.collapsible",{ 
        collapse:true 
        }) 
     .tablesorter({widgets: ['zebra'], sortMultiSortKey:'false', sortList: [[sortColumn,sortOrder]]}); 
    } 
} 

function toggleElement(element) { 
if ($("#" + element).attr("expanded") == "true"){ 
    $("#" + element).attr("expanded","false") 
    $("#" + element).hide(); 
    var isVisible = "false" 
} else { 
    $("#" + element).attr("expanded","true") 
    $("#" + element).show(); 
    var isVisible = "true" 
} 

//Rewrite the cookie for this section changing only the visibility 
var elementData = $.cookie(element); 
var visible = isVisible; 
if (elementData != null){ 
    var json = JSON.parse(elementData) 
    var id = json.ID; 
    var sortColumn = json.SortColumn; 
    var sortOrder = json.SortOrder; 
} else { 
    var id = element 
    var sortColumn = 0; 
    var sortOrder = 0; 
} 

var tableObj = new tableContainer(id,visible,sortColumn,sortOrder); 
$.cookie(element, JSON.stringify(tableObj), { secure: true }); //Write the current state into the section cookie 
}