JS使用cookie設(shè)置樣式的方法
本文實(shí)例講述了JS使用cookie設(shè)置樣式的方法。分享給大家供大家參考,具體如下:
var styleShow = ["blackgreen", "purple"];
var path = "/";
var StyleSwitch = {
//設(shè)置樣式
setStyleSheet: function (StyleName) {
var i, a, main;
for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
if (a.getAttribute("rel").indexOf("Style") != -1 && a.getAttribute("title")) {
a.disabled = true;
if (a.getAttribute("title") == StyleName) {
a.disabled = false;
this.setCookie("Style", StyleName);
}
}
}
},
//獲取當(dāng)前使用樣式
getStyleSheet: function () {
var i, a;
for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
if (a.getAttribute("rel").indexOf("Style") != -1 && a.getAttribute("title") && !a.disabled) {
return a.getAttribute("title");
}
}
return null;
},
//獲取默認(rèn)樣式
getPreferredStyleSheet: function () {
var i, a;
for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
if (a.getAttribute("rel").indexOf("Style") != -1
&& a.getAttribute("rel").indexOf("alt") == -1
&& a.getAttribute("title")
) {
return a.getAttribute("title");
}
}
return null;
},
//獲取cookie
getCookie: function (name) {
var cookieName = encodeURIComponent(name) + "=",
cookieStart = document.cookie.indexOf(cookieName),
cookieValue = null;
if (cookieStart > -1) {
var cookieEnd = document.cookie.indexOf(";", cookieStart);
if (cookieStart == -1) {
alert(-2);
cookieEnd = document.cookie.length;
}
cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd));
}
for (var i = 0; i < styleShow.length; i++) {
if (cookieValue == styleShow[i]) {
alert(styleShow[i]);
return styleShow[i];
}
}
return styleShow[0];
},
//生成cookie
//name cookie名
//value 值
//expires 過期時(shí)間
//path 路徑
setCookie: function (name, value, expires, path, domain, secure) {
var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value) + ";";
if (expires instanceof Date) {
cookieText += "expires=" + expires.toGMTString();
}
if (path) {
cookieText += ";path=" + path;
}
if (domain) {
cookieText += ";domain=" + domain;
}
if (secure) {
cookieText += ";secure";
}
document.cookie = cookieText;
},
//重置cookie
unsetCookie: function (name, path, domain, secure) {
this.set(name, "", new Date(0), path, domain, screen);
}
};
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript中json操作技巧總結(jié)》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動(dòng)畫特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
uniapp中解析markdown支持網(wǎng)頁和小程序?qū)崿F(xiàn)示例
這篇文章主要為大家介紹了uniapp中解析markdown支持網(wǎng)頁和小程序?qū)崿F(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
javascript 在網(wǎng)頁中的運(yùn)用(asp.net)
javascript在網(wǎng)頁中的運(yùn)用實(shí)現(xiàn),需要的朋友可以參考下。2009-11-11
JavaScript對象_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了JavaScript對象的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
JS實(shí)現(xiàn)黑色風(fēng)格的網(wǎng)頁TAB選項(xiàng)卡效果代碼
這篇文章主要介紹了JS實(shí)現(xiàn)黑色風(fēng)格的網(wǎng)頁TAB選項(xiàng)卡效果代碼,通過簡單的頁面元素遍歷控制頁面tab切換效果,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10

