首页 > 代码库 > Cookie

Cookie

    $(".theme-colors > li > span").hover(function(e) {        var t = $(this),        n = $("body");        n.attr("class", "").addClass("theme-" + t.attr("class"));    },    function() {        var e = $(this),        t = $("body");        t.attr("data-theme") !== undefined ? t.attr("class", "").addClass(t.attr("data-theme")) : t.attr("class", "")    }).click(function() {        var e = $(this);        $("body").addClass("theme-" + e.attr("class")).attr("data-theme", "theme-" + e.attr("class"));        addCookie(‘text_class‘, e.attr("class"), 86400 * 30);    });/cookie 开始/var addCookie = function(objName, objValue, objHours) {        var str = objName + "=" + escape(objValue);        if (objHours > 0) {            var date = new Date();            var ms = objHours * 3600 * 1000 * 24;            date.setTime(date.getTime() + ms);            str += ";path=/; expires=" + date.toGMTString();        }        document.cookie = str;    },    getCookie = function(objName) {        var arrStr = document.cookie.split("; ");        for (var i = 0; i < arrStr.length; i++) {            var temp = arrStr[i].split("=");            if (temp[0] == objName) return unescape(temp[1]);        }    },    class_ = getCookie(‘text_class‘);    class_ && $("body").addClass("theme-" + class_).attr("data-theme", "theme-" + class_);/cookie 结束/原本可以更换样式的名称 或者是一个样式一个css文件 ,到时候更换 css文件即可。 cookie 操作类 100%可用/////------------------------js cookie操作类-------------------begin   [edit by ygd]function addCookie(objName, objValue, objHours) {//添加cookie    var str = objName + "=" + escape(objValue);    if (objHours > 0) {//为0时不设定过期时间,浏览器关闭时cookie自动消失        var date = new Date();        var ms = objHours * 3600 * 1000 * 24;        date.setTime(date.getTime() + ms);        str += "; expires=" + date.toGMTString();    }    document.cookie = str;}function getCookie(objName) {//获取指定名称的cookie的值    var arrStr = document.cookie.split("; ");    for (var i = 0; i < arrStr.length; i++) {        var temp = arrStr[i].split("=");        if (temp[0] == objName) return unescape(temp[1]);    }}/////------------------------js cookie操作类-------------------end