John Giotta在前面(堆栈溢出)给了我这个代码,以便在我的网站上实现一个“关闭样式”按钮(用于大学作业)。

function nostyle() {
    for (i=0; i<document.styleSheets.length; i++) {
        void(document.styleSheets.item(i).disabled=true);
    }
}

我想知道实现一个cookie是多么容易,这样就可以记住这个javascript已经被应用了,这样网站上浏览过的每一个页面都关闭了样式(我的知识非常基础,我才刚满一年)。
当做

最佳答案

function SetCookie(cookieName,cookieValue,nDays) {
    var today = new Date();
    var expire = new Date();
    if (nDays==null || nDays==0) nDays=1;
    expire.setTime(today.getTime() + 3600000*24*nDays);
    document.cookie = cookieName+"="+escape(cookieValue)
        + ";expires="+expire.toGMTString();
}

设置您的css_disabledcookie
SetCookie('css_disabled', 'true', 100);

要读取cookie:
function ReadCookie(cookieName) {
    var theCookie=""+document.cookie;
    var ind=theCookie.indexOf(cookieName+"=");
    if (ind==-1 || cookieName=="") return "";
    var ind1=theCookie.indexOf(";",ind);
    if (ind1==-1) ind1=theCookie.length;
    return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

if (ReadCookie('css_disabled') == 'true') {...

10-02 20:30