jQuery 可以读/写 cookie 到浏览器吗? [英] Can jQuery read/write cookies to a browser?

查看:23
本文介绍了jQuery 可以读/写 cookie 到浏览器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单示例:我想在页面上放置一些项目(如 div 或表格行),并且我想让用户单击它们以选择它们.这在 jQuery 中似乎很容易.为了在没有服务器端回传的情况下保存用户点击的项目,我认为 cookie 是完成此操作的一种简单方法.

Simple example: I want to have some items on a page (like divs or table rows), and I want to let the user click on them to select them. That seems easy enough in jQuery. To save which items a user clicks on with no server-side post backs, I was thinking a cookie would be a simple way to get this done.

  1. 在这种情况下,cookie 没问题的假设是否正确?
  2. 如果它是正确的,jQuery API 是否有某种方式来读取/写入 cookie 信息,它比默认的 JavaScript API 更好?

推荐答案

用于设置 cookie 的默认 JavaScriptAPI"非常简单:

The default JavaScript "API" for setting a cookie is as easy as:

document.cookie = 'mycookie=valueOfCookie;expires=DateHere;path=/'

使用 jQuery cookie 插件,例如:

Use the jQuery cookie plugin like:

$.cookie('mycookie', 'valueOfCookie')

这篇关于jQuery 可以读/写 cookie 到浏览器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆