Currently, skin changing for websites is a common feature that is available in most forums. To achieve this effect, you can see the following code.
Html code:
1. To have a style sheet link with id, we need to call different href through this link.
2. Skin selection button (The onclick event is added for each li in the background to trigger skin replacement)
- Gray
- Green
- Yellow
- Blue
- Pink
- Purple
Js section:
1. Skin replacement method
// Set cookie, select button, page skin
Skin. setSkin = function (n ){
Var skins = ("skin"). getElementsByTagName ("li ");
For (I = 0; I {
Skins [I]. className = ""; // initialize the button status
}
Skin. setCookie (n); // Save the current style
("Skin _" + n). className = "selected"; // you can specify the style of the selected skin button.
("Cssfile"). href = "css/main" + n + ". css? 6.1.3 "; // set the Page Style
}
2. Access cookies
// Save the current skin n to the cookie
Skin. setCookie = function (n ){
Var expires = new Date ();
Expires. setTime (expires. getTime () + 24x60x60x365*1000 );
Var flag = "Skin_Cookie =" + n;
Document. cookie = flag + "; expires =" + expires. toGMTString ();
}
// Return the skin style set by the user
Skin. readCookie = function (){
Var skin = 0;
Var mycookie = document. cookie;
Var name = "Skin_Cookie ";
Var start1 = mycookie. indexOf (name + "= ");
If (start1 =-1 ){
Skin = 0; // if not set, the default style is displayed.
}
Else {
Var start = mycookie. indexOf ("=", start1) + 1;
Var end = mycookie. indexOf (";", start );
If (end =-1 ){
End = mycookie. length;
}
Var values = unescape (mycookie. substring (start, end ));
If (values! = Null)
{
Skin = values;
}
}
Return skin;
}
3. Bind a skin change button event
Skin. addEvent = function (){
Var skins = ("skin"). getElementsByTagName ("li ");
For (I = 0; I {
Skins [I]. onclick = function () {skin. setSkin (this. id. substring (5 ))};
}
}
4. Set skin style after page loading
Window. onload = function (){
Skin. setSkin (skin. readCookie (); // set the skin style based on the returned value of the read cookie.
Skin. addEvent (); // bind button event