且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jquery将json数据对象保存在cookie中

更新时间:2022-01-29 00:10:26

您可以将数据序列化为 JSON,如下所示:

You can serialize the data as JSON, like this:

$.cookie("basket-data", JSON.stringify($("#ArticlesHolder").data()));

然后从cookie中获取:

Then to get it from the cookie:

$("#ArticlesHolder").data(JSON.parse($.cookie("basket-data")));

这依赖于 JSON.stringify()JSON.parse() 序列化/反序列化你的数据对象,对于旧浏览器(IE<8)包括 json2.js 获取 JSON 功能.此示例使用 jQuery cookie 插件

This relies on JSON.stringify() and JSON.parse() to serialize/deserialize your data object, for older browsers (IE<8) include json2.js to get the JSON functionality. This example uses the jQuery cookie plugin