【问题标题】:jQuery cookies, set dynamic name to the cookiejQuery cookie,为 cookie 设置动态名称
【发布时间】:2012-12-05 00:05:37
【问题描述】:

我正在使用带有 jQ​​uery cookie 的 jQuery 选项卡。

我正在使用 jQuery cookie 来保存当前选定的选项卡,我的问题是我在我的项目中使用了很多选项卡,一旦选定的选项卡保存在 cookie 中,所有选项卡都会被保存.

有什么解决办法吗? 我正在考虑为每个页面设置动态 cookie 名称?但是,我该怎么做呢?

$('#tabs').tabs({
            cookie: {
                expires: 1,
                name: // set different cookie name for each #tabs
 }});​

非常感谢任何建议。

【问题讨论】:

标签: javascript jquery jquery-tabs jquery-cookie


【解决方案1】:

此时您无法将 name 设置为可调用,但您可以使用以下方法更改 cookie 选项(或传递给 .tabs 构造函数的任何其他选项):http://jqueryui.com/demos/tabs/#method-option

假设您已初始化选项卡,例如使用:

// intitialization
$( "#tabs" ).tabs({
    expires: 1, name: 'something'
});

当您需要更改 cookie 名称时,您可以这样做:

// when you want to change cookie option for that tabs
$("#tabs").tabs("option", "cookie", {expires: 1, name: 'booyah'})
// ... somewhere else in your code, change it again, just for fun
$("#tabs").tabs("option", "cookie", {expires: 1, name: 'grandma'})

【讨论】:

    【解决方案2】:

    设置 cookie name 似乎对我来说很好用:

    $('#tabs').tabs({
        cookie:{
            expires: 7,
            path: '/',
            name: '<some variable name>'
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      相关资源
      最近更新 更多