Alternative Style: Working With Alternate Style Sheets
原文地址:http://alistapart.com/article/alternate
1. Guide
So you’ve got a web page. You’ve marked it up with structural XHTML. You’ve also been a good little web developer and used style sheets to control what your document looks like. You’ve even gone the extra mile and created several alternative style sheets to show how hardcore you are.
2. Styling your site
Style sheets can be associated with documents using a list of link elements in the head. There are three different relationships external style sheets can have with the document: persistent, preferred, and alternate.
To make the style sheet paul.css persistent, the following link element would be included in the head:
<link rel="stylesheet" type="text/css" href="paul.css" />
To make paul.css preferred, a title attribute is added, giving the default style a name:
<link rel="stylesheet" type="text/css" href="paul.css" title="bog standard" />
To make paul.css into an alternate style sheet, the keyword “alternate” is added to the rel attribute:
<link rel="alternate stylesheet" type="text/css" href="paul.css" title="wacky" />
Note that these relationships only apply to external style sheets which are included using the link element.
3. Swappin' styles
Here’s where a little bit of JavaScript can be used along with the DOM to provide a way for MSIE and Mozilla users to select the style sheet they want to use. Their preference can also be stored in a cookie. And because we are using the link tags as the W3C tells us to, the JavaScript doesn’t interfere with the menu in Mozilla, and it degrades very gracefully.
4. The Script
1 function setActiveStyleSheet(title) { 2 var i, a, main; 3 for(i=0; (a = document.getElementsByTagName("link")[i]); i++) { 4 if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) { 5 a.disabled = true; 6 if(a.getAttribute("title") == title) a.disabled = false; 7 } 8 } 9 } 10 11 function getActiveStyleSheet() { 12 var i, a; 13 for(i=0; (a = document.getElementsByTagName("link")[i]); i++) { 14 if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title"); 15 } 16 return null; 17 } 18 19 function getPreferredStyleSheet() { 20 var i, a; 21 for(i=0; (a = document.getElementsByTagName("link")[i]); i++) { 22 if(a.getAttribute("rel").indexOf("style") != -1 23 && a.getAttribute("rel").indexOf("alt") == -1 24 && a.getAttribute("title") 25 ) return a.getAttribute("title"); 26 } 27 return null; 28 } 29 30 function createCookie(name,value,days) { 31 if (days) { 32 var date = new Date(); 33 date.setTime(date.getTime()+(days*24*60*60*1000)); 34 var expires = "; expires="+date.toGMTString(); 35 } 36 else expires = ""; 37 document.cookie = name+"="+value+expires+"; path=/"; 38 } 39 40 function readCookie(name) { 41 var nameEQ = name + "="; 42 var ca = document.cookie.split(';'); 43 for(var i=0;i < ca.length;i++) { 44 var c = ca[i]; 45 while (c.charAt(0)==' ') c = c.substring(1,c.length); 46 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); 47 } 48 return null; 49 } 50 51 window.onload = function(e) { 52 var cookie = readCookie("style"); 53 var title = cookie ? cookie : getPreferredStyleSheet(); 54 setActiveStyleSheet(title); 55 } 56 57 window.onunload = function(e) { 58 var title = getActiveStyleSheet(); 59 createCookie("style", title, 365); 60 } 61 62 var cookie = readCookie("style"); 63 var title = cookie ? cookie : getPreferredStyleSheet(); 64 setActiveStyleSheet(title);
5. Apply
To allow the visitor to change the active style sheet, you could use javascript onClick events.
Once the visitor has selected a theme, it will be stored in a cookie. To use the same theme throughout your website, the same style sheet and javascript link elements should be included in the head of every page of the site.
浙公网安备 33010602011771号