搞了一中午 !没想到这段代码这么好用! 大家来分享下!交流交流! 想请教会用cookies的朋友指点下怎么调用cookies! 效果http://www.likechong.com/index.html 主要代码: <link id=csss rel="stylesheet" type="text/css" href="skin/css.css"> </head> <body> 选择网站风格 <input type=button class="inputtj1" value="红红圣诞" onclick="document.all.csss.href='skin/css.css'"> <input type=button class="inputtj1" value="紫色浪漫" onclick="document.all.csss.href='skin/csszsd.css'"> <input type=button class="inputtj1" value="蓝色简约" onclick="document.all.csss.href='skin/cssold.css'">
前段时间也看到一段关于这个功能的脚本,不过要繁琐多了。可以借鉴一下。 Head 区域: 代码: <link rel="stylesheet" type="text/css" href="paul.css" title="bog standard" /> <link rel="alternate stylesheet" type="text/css" href="paul.css" title="wacky" /> <script type="text/javascript" src="styleswitcher.js"></script> 控制部分: 代码: <a href="#" onclick="setActiveStyleSheet('default');return false;">change style to default</a> <a href="#" onclick="setActiveStyleSheet('paul');return false;">change style to paul</a> styleswitcher.js PHP: function setActiveStyleSheet(title) { 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") == title) a.disabled = false; } } } function getActiveStyleSheet() { 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; } function getPreferredStyleSheet() { 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; } function createCookie(name,value,days) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); } else expires = ""; document.cookie = name+"="+value+expires+"; path=/"; } function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } window.onload = function(e) { var cookie = readCookie("style"); var title = cookie ? cookie : getPreferredStyleSheet(); setActiveStyleSheet(title); } window.onunload = function(e) { var title = getActiveStyleSheet(); createCookie("style", title, 365); } var cookie = readCookie("style"); var title = cookie ? cookie : getPreferredStyleSheet(); setActiveStyleSheet(title);
谢谢!"不学无术"给的代码!果然不错!我放上去,测试了下真的有效果!可以用 COOKIE记录并保持用户选择的风格, 其中控制部分的onclick="setActiveStyleSheet('paul');return 中的paul要对应Head 区域的 title="