jquery.cookie中的操作之與換膚

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 cookieoptions = options || {};if (value === null) {value = '';options.expires = -1;}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 * 24 * 60 * 60 * 1000));} 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 cookievar cookieValue = null;if (document.cookie && document.cookie != '') {var cookies = document.cookie.split(';');for (var i = 0; i < cookies.length; i++) {var cookie = jQuery.trim(cookies[i]);// Does this cookie string begin with the name we want?if (cookie.substring(0, name.length + 1) == (name + '=')) {cookieValue = decodeURIComponent(cookie.substring(name.length + 1));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: 10 });});var cookie_skin = $.cookie( "MyCssSkin");//將MyCssSkin這個cookie值this.id賦給變量cookie_skinif (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: 10 });}})

版本一不好的地方,就是有大量的重復代碼,可以模塊化,函數化,來看版本二:

//網站換膚
$(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: 10 });
}

下面代碼也是換膚的例子

html:

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

皮膚路徑 :

js代碼:

var _skinId = getCookie("_skinId") ? getCookie("_skinId"):"1";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(0);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(0);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()+24*60*60*1000*365);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 < 0)    return null;iBegin += sTag.length;var iEnd = sCookie.indexOf(";", iBegin);if (iEnd < 0)    iEnd = sCookie.length;return sCookie.substring(iBegin, iEnd);
}function delCookie(sKey) 
{var tNow = new Date();setCookie(sKey, "", null, null, tNow.toGMTString(), null);
}

?

轉載于:https://www.cnblogs.com/shy1766IT/p/5189604.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/272356.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/272356.shtml
英文地址,請注明出處:http://en.pswp.cn/news/272356.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

51單片機—串口通信

計算機串行通信&#xff1a; 并行通信&#xff1a; 串行通信&#xff1a; 異步通信&#xff1a; 同步通信&#xff1a; 串行通信的傳輸方向&#xff1a; 串行通信常見的錯誤校驗&#xff1a; 傳輸速率比特率&#xff08;波特率&#xff09;&#xff1a; &#xff08;fos…

UI基礎:UILabel.UIFont

UILabel:標簽 繼承自UIView ,在UIView基礎上擴充了顯示文本的功能.(文本框) UILabel的使用步驟 1.創建控件 UILabel *aLabel[[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 100)]; 2.設置屬性 (1).設置背景顏色 aLabel.backgroundColor[UIColor cyanColor]; (2)設…

IIC總線通訊協議、EEPROM芯片

EEPROM芯片&#xff1a; 掉電不會丟失數據&#xff0c;可以保存數據。 IIC串行總線的組成及工作原理&#xff1a; IIC總線傳輸協議 IIC產生起始與終止信號&#xff1a; IIC字節的傳送與應答&#xff1a; 應答位作用&#xff1a; 數據幀格式&#xff1a; 總線尋址 軟件模…

【Xamarin挖墻腳系列:最重要的布局ListView】

安卓的幾個重要的布局 線性布局 相對布局 Table布局 Tab布局 表格Grid布局 列表布局。 這幾種基本的布局的方式&#xff0c;最重要的是列表布局。任何一個程序&#xff0c;基本都可以劃分為 3點一線模式&#xff08;類別 列表 詳細&#xff09;&#xff0c;我個人稱呼它為CLD…

STM32F1 GPIO工作原理初探

GPIO工作方式&#xff1a; 1、四種輸入模式 輸入浮空 輸入上拉 輸入下拉 模擬輸入 2、四種輸出模式 開漏輸出&#xff1a; 只可以輸出強低電平&#xff0c;高電平得靠外部電阻拉高。輸出端相當于三極管的集電極&#xff0c;要得到高電平狀態需要上拉電阻才行&#xff0…

Js基礎知識梳理系列

小序&#xff1a;總是感覺自己的技術一直在原地踏步&#xff0c;想學習一些新的技術&#xff0c;但學起來很吃力&#xff0c;而且總是沒有什么實際的收獲&#xff0c;似乎進入了所謂的“瓶頸期”。問了一些前輩是否也遇到過同樣的問題&#xff0c;他們給我的解決方案是&#xf…

STM32F103ZET6 點燈的三種操作方式(庫函數、寄存器、位操作)

LED硬件連接&#xff1a; 點燈的基本步驟&#xff1a; 庫函數版本 重要函數&#xff1a; main.c部分&#xff1a; #include "stm32f10x.h" #include "LED.h" #include "delay.h"int main(void) {LED_Init();//GPIOB、E初始化delay_init()…

redis的分布式解決方式--codis

codis是豌豆莢開源的分布式server。眼下處于穩定階段。 原文地址&#xff1a;https://github.com/wandoulabs/codis/blob/master/doc/tutorial_zh.md Codis 是一個分布式 Redis 解決方式, 對于上層的應用來說, 連接到 Codis Proxy 和連接原生的 Redis Server 沒有明顯的差別 (不…

STM32F103ZET6 蜂鳴器、按鍵

蜂鳴器的硬件電路&#xff1a; 蜂鳴器實驗步驟&#xff1a; 實驗步驟基本和跑馬燈一樣&#xff0c;代碼和跑馬燈也基本一樣&#xff0c;只是用的GPIO不同。 幾種輸入輸出模式&#xff1a; beep.c部分代碼&#xff1a; #include "beep.h" #include "stm32f1…

MDK寄存器地址映射分析

在51單片機中&#xff1a; 首先我們看看 51 中是怎么做的。51 單片機開發中經常會引用一個 reg51.h 的頭文件&#xff0c;下面我們看看他是怎么把名字和寄存器聯系起來的&#xff1a; sfr P0 0x80;sfr 也是一種擴充數據類型&#xff0c;點用一個內存單元&#xff0c;值域為 0&…

Mysql多表查詢(兩張獨立表,一張關系表)

一、數據庫設計1、三個數據表長這樣其中user表記錄用戶信息&#xff0c;cat主要記錄男女性別&#xff0c;mete表是用戶id和性別id的對應關系2、具體數據如下二、查詢目標查詢出所有性別為“男”的用戶的“姓名”&#xff0c;如下記錄兩種不同形式的查詢1、單純的條件查詢SQL&am…

STM32 時鐘系統

STM32時鐘系統的基本概念 概念及意義 &#xff08;1&#xff09;概念&#xff1a;時鐘系統是由振蕩器&#xff08;信號源&#xff09;、定時喚醒器、分頻器等組成的電路。常用的信號源有晶體振蕩器和RC振蕩器。 &#xff08;2&#xff09;意義&#xff1a;時鐘對數字電路而言非…

【轉載】性能測試淺談

本文主要針對WEB系統的性能測試。不涉及具體的執行操作&#xff0c;只是本人對性能測試的一點理解和認識。 性能測試的目的&#xff0c;簡單說其實就是為了獲取待測系統的響應時間、吞吐量、穩定性、容量等信息。而發現一些具體的性能相關的缺陷&#xff08;如內存溢出、并發處…

SystemInit時鐘系統初始化函數剖析

SystemInit&#xff08;&#xff09;函數&#xff1a; void SystemInit (void) {/* Set HSION bit */RCC->CR | (uint32_t)0x00000001;//把內部的HSI RC&#xff08;高速時鐘&#xff09;打開#ifndef STM32F10X_CLRCC->CFGR & (uint32_t)0xF8FF0000;//這一句不會執行…

火狐表格錯亂兼容性問題

對于某一單元行需要顯示時&#xff0c;使用CSS display:block屬性&#xff0c;不需要顯示時使用display:none屬性&#xff0c;在IE瀏覽器中顯示正常&#xff0c;沒有任何問題&#xff0c;但是當用Firefox瀏覽時卻出現了布局錯亂的問題&#xff0c;這是為什么呢&#xff1f; 本文…

docker ps命令詳解 列出運行中的容器

docker ps命令詳解 列出運行中的容器 使用docker ps命令即可列出運行中的容器&#xff0c;執行該命令后&#xff0c;會出現如下7列表格 CONTAINER_ID 表示容器ID IMAGE 表示鏡像名稱 COMMAND 表示啟動容器時運行的命令 CREATED …

Lattice 的 Framebuffer IP核使用調試筆記之datasheet筆記

本文由遠航路上ing 原創&#xff0c;轉載請標明出處。 學習使用以及調試Framebuffer IP 核已經有一段時間了&#xff0c;調試的時候總想記錄些東西&#xff0c;可是忙的時候就沒有時間來寫&#xff0c;只有先找個地方記錄下&#xff0c;以后再總結。所以找這個時間好好的記錄學…

Systick滴答定時器寄存器、delay()延時函數、SysTick_Config函數

SysTick定時器 SysTick定時器&#xff0c;是一個簡單的定時器&#xff0c;對于CM3、CM4內核的芯片都有SysTick定時器。SysTick 是一個 24 位的倒計數定時器&#xff0c;當計數到 0 時&#xff0c;將從RELOAD 寄存器中自動重裝載定時初值&#xff0c;開始新一輪計數。只要不把它…

查看docker容器日志

1&#xff1a;實時查看docker容器id為 02c5ac132ee5 的最后10行日志 docker logs -f -t --tail 10 02c5ac132ee5 2:查看指定時間后的日志&#xff0c;只顯示最后100行&#xff1a; docker logs -f -t --since"2020-02-14" --tail100 d7db22166a0a 3:查看最近20分鐘的…

Web UI 自動化測試環境搭建 (轉載自51測試天地第三十九期上)

1. 安裝 Python 2.7 并設置系統環境變量 2. 下載并安裝 python setuptools Easily download, build, install, upgrade, and uninstall Python packages https://pypi.python.org/pypi/setuptools#installation-instructions 2.1 找到ez_setup.py&#xff0c;點擊右鍵--目標另存…