A-A+
JQUERY.COOKIE插件的基本用法
JQUERY.COOKIE是一个集成了js的cookie操作的一个插件了,此插件使用简单并且可以达到对cookie读写删除操作,下文为各位深入的介绍。
jQuery.cookie的基本用法:
- Include script after the jQuery library (unless you are packaging scripts somehow else):
- <script src="/path/to/jquery.cookie.js"></script>
- Create session cookie:
- $.cookie('name', 'value');
- Create expiring cookie, 7 days from then:
- $.cookie('name', 'value', { expires: 7 });
- Create expiring cookie, valid across entire site:
- $.cookie('name', 'value', { expires: 7, path: '/' });
- Read cookie:
- $.cookie('name'); // => "value"
- $.cookie('nothing'); // => undefined
- Read all available cookies:
- $.cookie(); // => { "name": "value" }
- Delete cookie:
- // Returns true when cookie was successfully deleted, otherwise false
- $.removeCookie('name'); // => true
- $.removeCookie('nothing'); // => false
- // Need to use the same attributes (path, domain) as what the cookie was written with
- $.cookie('name', 'value', { path: '/' });
- // This won't work!
- $.removeCookie('name'); // => false
- // This will work!
- $.removeCookie('name', { path: '/' }); // => true
JQUERY的COOKIE实例
jquery的cookie插件可以方便操作cookie,首先引入jquery.cookie.js文件,可以去官网下载。
- <script type="text/javascript" src="__STATIC__/common/js/jquery.cookie.js"></script>
以下使用jquery的cookie实现一个简单的功能,一个公共栏,可以点击展开或者收缩,第一次进入页面默认展开,第二次以后进入页面默认收缩,但是点击也可以展开。
通过jquery的cookie记录是否第一次访问界面:
- <script>
- (function($) {
- var $trigger = $('.dz_box .dz_title');
- var $view_cur = $('.dz_title a:first');
- var $view = $('.dz_box .dz_info');
- var uid = {$businesser_info['uid']};
- var has_read = $.cookie('gonggao_uid_' + uid);
- if (has_read) {
- $view.hide();
- $view_cur.removeClass('wel_down01').addClass('wel_up01');
- }else{
- $view_cur.removeClass('wel_up01').addClass('wel_down01');
- $.cookie('gonggao_uid_' + uid, '1');
- }
- $trigger.click(function () {
- if ($view_cur.hasClass('wel_up01')) {
- $view.slideDown();
- $view_cur.removeClass('wel_up01').addClass('wel_down01');
- } else {
- $view.slideUp();
- $view_cur.removeClass('wel_down01').addClass('wel_up01');
- }
- });
- })(jQuery);
- </script>