How to realize multi-style selection and real-time switching of styles_Experience exchange

不言
Release: 2018-05-16 14:42:08
Original
2829 people have browsed it

when we make a website, we hope that our website will have multiple styles. users can choose different styles according to their own preferences. such styles can be changes in layout, differences in color, or it is a style specially customized for different user groups.
how can we achieve multi-style selection and real-time switching of styles?
in fact, ie does not support this function. we can leave it to the browser. firefox supports this function.
suppose we have two sets of css, enclosed in two different files: a.css and b.css. then add the following two lines of xhtml code between

and :
<link rel="stylesheet" type="text/css" title="主题a" href="a.css?7.1.34" /> 
<link rel="alternate stylesheet" type="text/css" title="主题b" href="b.css?7.1.34" />
Copy after login

then open this page with your firefox and select in the menu bar: view-> page style, it should you can see "theme a" and "theme b" and select them in real time.
another method we can use is to use dynamic programs, such as asp, php, jsp, etc. the advantages of this are direct, efficient, good compatibility, and the ability to remember user choices. the user's selection can be recorded in cookies or directly written to the database. when the user visits again, the style selected on the previous visit will be directly called. we won’t go into details about the specific production here. you can pay attention to our website www.52css.com. we will launch content in this area from time to time.
what method should we use now? the mainstream browser ie does not support the method of letting the browser choose. how to implement it with a program script? when my webpage is static, there is no database.
we can only choose to use javascript to get it done. let’s look at the following code:

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 = ""; 
documents.cookie = name+"="+value+expires+"; path=/"; 
} 
function readcookie(name) { 
var nameeq = name + "="; 
var ca = documents.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);
Copy after login

the above code is a javascript script that implements multi-style selection and real-time style switching. we can save the above code as a js file and display it on the required page. direct quote:

<script type="text/javascript" src="cssturn.js?7.1.34"></script>
Copy after login

of course, you can also write the above code directly inside the page.
we have three styles, one defaults to the other two styles. introduce these three css files into the page file:

<link rel="stylesheet" type="text/css" href="css.css?7.1.34" /> 
<link rel="stylesheet" type="text/css" href="aaa.css?7.1.34" title="aaa" /> 
<link rel="stylesheet" type="text/css" href="bbb.css?7.1.34" title="bbb" />
Copy after login

ok, we can now add a link to switch styles in the page:

<a href="#" onclick="setactivestylesheet('',1); return false;">默认样式-白色</a> 
<a href="#" onclick="setactivestylesheet('aaa',1); return false;">样式一-蓝色</a> 
<a href="#" onclick="setactivestylesheet('bbb',1); return false;">样式二-橙色</a>
Copy after login

now that we are done, let’s test our results above and see the effect.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "<a rel="nofollow" href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" target="_blank">http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd</a>"> 
<html xmlns="<a rel="nofollow" href="http://www.w3.org/1999/xhtml" target="_blank">http://www.w3.org/1999/xhtml</a>"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>阿Q家园</title> 
<link rel="stylesheet"  type="text/CSS" href="<a rel="nofollow" href="http://www.52css.com/attachments/month_0701/r2007128164252.css?7.1.34" target="_blank">http://www.52css.com/attachments/month_0701/r2007128164252.css</a>"  /> 
<link rel="stylesheet" type="text/CSS" href="<a rel="nofollow" href="http://www.52css.com/attachments/month_0701/c2007128164223.css?7.1.34" target="_blank">http://www.52css.com/attachments/month_0701/c2007128164223.css</a>" title="aaa" /> 
<link rel="stylesheet" type="text/CSS" href="<a rel="nofollow" href="http://www.52css.com/attachments/month_0701/h2007128164239.css?7.1.34" target="_blank">http://www.52css.com/attachments/month_0701/h2007128164239.css</a>" title="bbb" /> 
<script type="text/javascript"> 
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 = ""; 
  documents.cookie = name+"="+value+expires+"; path=/"; 
} 

function readCookie(name) { 
  var nameEQ = name + "="; 
  var ca = documents.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); 
</script> 
</head> 
<body> 
<a href="#" onclick="setActiveStyleSheet('',1); return false;">默认样式-白色</a> 
<a href="#" onclick="setActiveStyleSheet('aaa',1); return false;">样式一-蓝色</a> 
<a href="#" onclick="setActiveStyleSheet('bbb',1); return false;">样式二-橙色</a> 
<p></p> 
</body> 
</html>
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template