jquery.cookie.js的插件,插件的源代码如下:

/**
* Cookie plugin
*
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/ /**
* Create a cookie with the given name and value and other optional parameters.
*
* @example $.cookie('the_cookie', 'the_value');
* @desc Set the value of a cookie.
* @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
* @desc Create a cookie with all available options.
* @example $.cookie('the_cookie', 'the_value');
* @desc Create a session cookie.
* @example $.cookie('the_cookie', null);
* @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
* used when the cookie was set.
*
* @param String name The name of the cookie.
* @param String value The value of the cookie.
* @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
* @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
* If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
* If set to null or omitted, the cookie will be a session cookie and will not be retained
* when the the browser exits.
* @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
* @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
* @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
* require a secure protocol (like HTTPS).
* @type undefined
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/ /**
* Get the value of a cookie with the given name.
*
* @example $.cookie('the_cookie');
* @desc Get the value of a cookie.
*
* @param String name The name of the cookie.
* @return The value of the cookie.
* @type String
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/ jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options.expires = -;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * * * * ));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
}
// CAUTION: Needed to parenthesize options.path and options.domain
// in the following expressions, otherwise they evaluate to undefined
// in the packed version for some reason...
var path = options.path ? '; path=' + (options.path) : '';
var domain = options.domain ? '; domain=' + (options.domain) : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else { // only name given, get cookie
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = ; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(, name.length + ) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + ));
break;
}
}
}
return cookieValue;
}
};

Cookie插件的操作

创建一个会话cookie:

$.cookie(‘cookieName’,'cookieValue’);

注:当没有指明cookie时间时,所创建的cookie有效期默认到用户浏览器关闭止,故被称为会话cookie。

创建一个持久cookie:

$.cookie(‘cookieName’,'cookieValue’,{expires:7});

注:当指明时间时,故称为持久cookie,并且有效时间为天。

创建一个持久并带有效路径的cookie:

$.cookie(‘cookieName’,'cookieValue’,{expires:7,path:’/'});

注:如果不设置有效路径,在默认情况下,只能在cookie设置当前页面读取该cookie,cookie的路径用于设置能够读取cookie的顶级目录。

创建一个持久并带有效路径和域名的cookie:

$.cookie(‘cookieName’,'cookieValue’,{expires:7,path:’/',domain: ‘chuhoo.com’,secure: false,raw:false});

注:domain:创建cookie所在网页所拥有的域名;secure:默认是false,如果为true,cookie的传输协议需为https;raw:默认为false,读取和写入时候自动进行编码和解码(使用encodeURIComponent编码,使用decodeURIComponent解码),关闭这个功能,请设置为true。

获取cookie:

$.cookie(‘cookieName’);   //如果存在则返回cookieValue,否则返回null。

删除cookie:

$.cookie(‘cookieName’,null);

或者这样也能删除: $.cookie('cookieName', '', { expires: -1, path: '/' }); // 删除 cookie

注:如果想删除一个带有效路径的cookie,如下:$.cookie(‘cookieName’,null,{path:’/'});

来演示一个换肤的粟子:

代码之div+css

<link rel="stylesheet" href="styles/skin/skin_0.css" type="text/css" id="cssfile" />

<!--头部开始-->
<div id="header">
<a id="logo" href="#">Jane Shopping</a>
<ul id="skin">
<li id="skin_0" title="蓝色" class="selected">蓝色</li>
<li id="skin_1" title="紫色">紫色</li>
<li id="skin_2" title="红色">红色</li>
<li id="skin_3" title="天蓝色">天蓝色</li>
<li id="skin_4" title="橙色">橙色</li>
<li id="skin_5" title="淡绿色">淡绿色</li>
</ul> </div>
<!--头部结束-->
/*切换皮肤样式*/
#skin {
float:right;
margin:10px;
padding:4px;
width:120px;
list-style:none;
border: 1px solid #CCCCCC;
background:#FFF;
}
#skin li {
float:left;
margin-right:4px;
width:15px;
height:15px;
text-indent:-9999px;
overflow:hidden;
display:block;
cursor:pointer;
background-image:url(../images/theme.gif);
}
#skin_0 { background-position:0px 0px; }
#skin_1 { background-position:15px 0px; }
#skin_2 { background-position:35px 0px; }
#skin_3 { background-position:55px 0px; }
#skin_4 { background-position:75px 0px; }
#skin_5 { background-position:95px 0px; }
#skin_0.selected { background-position:0px 15px; }
#skin_1.selected { background-position:15px 15px; }
#skin_2.selected { background-position:35px 15px; }
#skin_3.selected { background-position:55px 15px; }
#skin_4.selected { background-position:75px 15px; }
#skin_5.selected { background-position:95px 15px; }

不同皮肤的css文件都放在styles/skins 文件夹下,命名如下:

代码之jQuery代码

版本一:

        $(function(){
var $li =$("#skin li");
$li.click(function(){
$("#"+this.id).addClass("selected") //当前<li>元素选中
.siblings().removeClass("selected"); //去掉其它同辈<li>元素的选中
$("#cssfile").attr("href","styles/skin/"+ (this.id) +".css"); //设置不同皮肤
$.cookie( "MyCssSkin" , this.id , { path: '/', expires: });
});
var cookie_skin = $.cookie( "MyCssSkin");//将MyCssSkin这个cookie值this.id赋给变量cookie_skin
        if (cookie_skin) {
$("#"+cookie_skin).addClass("selected") //当前<li>元素选中
.siblings().removeClass("selected"); //去掉其它同辈<li>元素的选中
$("#cssfile").attr("href","styles/skin/"+ cookie_skin +".css"); //设置不同皮肤
$.cookie( "MyCssSkin" , cookie_skin , { path: '/', expires: });
}
})

版本一不好的地方,就是有大量的重复代码,可以模块化,函数化,来看版本二:

//网站换肤
$(function(){
var $li =$("#skin li");
$li.click(function(){
switchSkin( this.id );
});
var cookie_skin = $.cookie("MyCssSkin");
if (cookie_skin) {
switchSkin( cookie_skin );
}
}); function switchSkin(skinName){
$("#"+skinName).addClass("selected") //当前<li>元素选中
.siblings().removeClass("selected"); //去掉其他同辈<li>元素的选中
$("#cssfile").attr("href","styles/skin/"+ skinName +".css"); //设置不同皮肤
$.cookie( "MyCssSkin" , skinName , { path: '/', expires: });
}

下面代码也是换肤的例子

html:

    <script>
//加载皮肤文件
loadCss(_skinId,'Main.css');
</script> <div style="right:10px; top: 5px; position: absolute; height: 21px; text-align:right">
<a>皮肤:</a><a href="#" onclick="loadSkin('1')">藤蔓绿</a>&nbsp;<a href="#" onclick="loadSkin('2')">经典蓝</a>&nbsp;<a href="#" onclick="loadSkin('3')">甜蜜橙</a>&nbsp;<a href="#" onclick="loadSkin('4')">淡雅灰</a>
</div>

皮肤路径 :

js代码:

var _skinId = getCookie("_skinId") ? getCookie("_skinId"):"";

function loadSkin(skinId)
{
writeCookie("_skinId",skinId);
top.window.location.reload();
} function $(id)
{
return document.getElementById(id);
} function loadCss(skinId,cssName)
{
var head = document.getElementsByTagName('head').item();
css = document.createElement('link');
css.href = "Skin/Skin" + skinId + "/"+cssName;
css.rel = 'stylesheet';
css.type = 'text/css';
css.id = 'loadCss';
head.appendChild(css);
} function loadJs(file)
{
var scriptTag = document.getElementById('loadScript');
var head = document.getElementsByTagName('head').item();
if(scriptTag) head.removeChild(scriptTag);
script = document.createElement('script');
script.src = "../js/mi_"+file+".js";
script.type = 'text/javascript';
script.id = 'loadScript';
head.appendChild(script);
} function setCookie(sKey, sValue, sDomain, sPath, sExpires, blSecure)
{
var sCookieStr = sKey + "=" + sValue + ";"; if (sDomain) sCookieStr += " DOMAIN=" + sDomain + ";";
if (sPath) sCookieStr += " PATH=" + sPath + ";";
if (sExpires) sCookieStr += " EXPIRES=" + sExpires + ";";
if (blSecure) sCookieStr += " SECURE"; document.cookie = sCookieStr;
} function writeCookie(key,value)
{
var sExpires=new Date();
sExpires.setTime(sExpires.getTime()+****);
setCookie(key,value,"","",sExpires.toGMTString(),"");
if(getCookie(key) == null)
{
alert("您的浏览器安全设置过高,不支持Cookie,请重新设置浏览器的。");
}
} function getCookie(sKey)
{
var sCookie = document.cookie;
if( !sCookie ) return null; var sTag = sKey + "="; var iBegin = sCookie.indexOf(sTag);
if (iBegin < ) return null; iBegin += sTag.length; var iEnd = sCookie.indexOf(";", iBegin);
if (iEnd < ) iEnd = sCookie.length; return sCookie.substring(iBegin, iEnd);
} function delCookie(sKey)
{
var tNow = new Date();
setCookie(sKey, "", null, null, tNow.toGMTString(), null);
}

jquery.cookie中的操作之与换肤的更多相关文章

  1. jquery.cookie中的操作

    http://w3school.com.cn/js/js_cookies.asp jquery.cookie中的操作: jquery.cookie.js是一个基于jquery的插件,点击下载! 创建一 ...

  2. (转)jquery.cookie中的操作

      jquery.cookie中的操作: jquery.cookie.js是一个基于jquery的插件,点击下载! 创建一个会话cookie: $.cookie(‘cookieName’,'cooki ...

  3. 5.2 《锋利的jQuery》jQuery对表格的操作(选项卡/换肤)

    表格隔行变色以及单选/复选 表格展开关闭 表格筛选 字体变大/缩小 选项卡 网页换肤 tip1: $("tr:odd")和$("tr:even")选择器索引是从 ...

  4. vue中利用scss实现整体换肤和字体大小设置

    一.前言 利用Sass预处理实现换肤和字体大小调整. 思路及达到的效果:字体大小的适配使用window.devicePixelRatio的值和需要调整的差量进行控制.页面初始化是的字体适配可以根据de ...

  5. Android中插件开发篇之----应用换肤原理解析

    一.前言 今天又到周末了,感觉时间过的很快呀.又要写blog了.那么今天就来看看应用的换肤原理解析.在之前的一篇博客中我说道了Android中的插件开发篇的基础:类加载器的相关知识.没看过的同学可以转 ...

  6. vue 中使用sass实现主体换肤

    有如下代码要实现换肤功能 <template> <div class="app-root" :class="themeClass"> & ...

  7. jQuery + Cookie引导客户操作

    网址:http://www.sucaihuo.com/js/707.html 示例:http://www.sucaihuo.com/jquery/7/707/demo/

  8. jquery.cookie 使用文档,$.cookie() 文档教程, js 操作 cookie 教程文档。

    jquery.cookie 使用文档,$.cookie() 文档教程, js 操作 cookie 教程文档. jquery.cookie中的操作: jquery.cookie.js是一个基于jquer ...

  9. 管理Cookie的插件——jquery.cookie.js

    下载地址:http://plugins.jquery.com/cookie/ jquery.cookie中的操作: 一.创建cookie: 1.创建一个会话cookie: $.cookie('cook ...

随机推荐

  1. iOS获取已安装的app列表(私有库)+ 通过包名打开应用

    1.获取已安装的app列表 - (void)touss { Class lsawsc = objc_getClass("LSApplicationWorkspace"); NSOb ...

  2. PTC介绍

    付费点击或按就付(英文:Paid-To-Click,缩写作PTC或pay_per-click)是一种点击付费的线上商业模式.PTC的经营模式是以PTC网站作为广告客户和消费者的仲介,广告客户付钱给经营 ...

  3. 万里长征第二步——django个人博客(第三步 —— 设置一些全局变量)

    可以将一些全局变量设置在settingg.py里 #网站的基本信息配置 SITE_NAME = 'John的个人博客' SITE_DESC = '专注学习Python开发,欢迎和大家交流' WEIBO ...

  4. DevExpress 小计 GridControl 隔行换行

    摘自: http://www.cnblogs.com/yuerdongni/archive/2012/09/08/2676753.html 1. 如何解决单击记录整行选中的问题 View->Op ...

  5. R简易安装

    post={"title":"my Blog post","content":"Here's my blog post" ...

  6. 北邮连接bupt-mobile

    内容源自:[伪攻略]电脑(win10)连接BUPT-mobile教程 1.控制面板:控制面板\网络和 Internet\网络连接 右键——属性,记住网络配置器的名字(划线部分) 点击配置——高级——网 ...

  7. 【笔记】关于require.js 的用法

    最近忙于学校的一个新网站建设,对于以前的前端程序编写方式的不正规特意上网学习了require.js 的用法,使此次的工程更加有条理同时符合当前前端的开发模式——前端模块化. 网上有不少很好的学习文章这 ...

  8. 【笔记】探索js 的this 对象 (第二部分)

    了解this 对象之后 我们明白了this 对象就是指向调用函数的作用域 那么接下来我们便要清除函数究竟在哪个作用域调用 找到调用的作用域首先要了解一下几点: 1.调用栈: 调用栈就是一系列的函数,表 ...

  9. Netty Client和Server端实现

    本文基于Nett4.0.26.Final版本浅析Client与Server端通讯,先看服务器端: public class Server { public static void run(int po ...

  10. Discuz常见小问题2-如何修改整个网站的默认字体为微软雅黑

    界面-风格管理,然后点击默认模板的编辑,在正常字体和小号字体前面加上你要的字体(比如微软雅黑,XXX,XXX),挨个排到后面,如果前面的字体没有则显示后面的 修改之后的效果(注意你不要在页面定义别的C ...