本文实例讲述了jQuery cookie的公共方法封装和使用。分享给大家供大家参考,具体如下:
common.js放入公共方法:
/** * 刷新cookie */ var cookiesPath = '/'; var cookiesTime = 3 / 24; function refreshCookie() { var cookieData = $.cookie(); $.each(cookieData, function (_key, _value) { $.cookie(_key, _value, {path: cookiesPath, expires: cookiesTime}); }); }/** * 刷新cookie */ var cookiesPath = '/'; var cookiesTime = 3 / 24; function refreshCookieCopy() { var cookieData = _$.cookie(); _$.each(cookieData, function (_key, _value) { _$.cookie(_key, _value, {path: cookiesPath, expires: cookiesTime}); }); } /** * 设置cookie * @param key * @param value */ function setCookie(key, value) { refreshCookie(); $.cookie(key, value, {path: cookiesPath, expires: cookiesTime}); } /** * 设置_cookie * @param key * @param value */ function setCookieCopy(key, value) { refreshCookieCopy(); _$.cookie(key, value, {path: cookiesPath, expires: cookiesTime}); } /** * 设置cookie by time * @param key * @param value */ function setCookieByTime(key, value, time) { // refreshCookie(); $.cookie(key, value, {path: cookiesPath, expires: time}); } /** * 获取cookie * @param key */ function getCookie(key) { return $.cookie(key); } /** * 删除cookie * @param key */ function deleteCookie(key) { $.removeCookie(key, {path: cookiesPath}); } /** * 清除cookie */ function clearCookie() { var cookieData = $.cookie(); $.each(cookieData, function (key, value) { deleteCookie(key); }); } /** * 清除单个cookie */ function clearCookieSingle(name) { setCookie(name, "", -1); }
如何使用:
function useCookie() { setCookie("name","gaopian"); getCookie("name"); deleteCookie("name"); clearCookie(); }
更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery的cookie操作技巧总结》、《jQuery扩展技巧总结》、《jQuery常用插件及用法总结》、《jQuery表格(table)操作技巧汇总》、《jQuery常见经典特效汇总》及《jquery选择器用法总结》
希望本文所述对大家jQuery程序设计有所帮助。