六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 35|回复: 0

Javascript 动态切换样色表

[复制链接]

升级  49.33%

32

主题

32

主题

32

主题

秀才

Rank: 2

积分
124
 楼主| 发表于 2013-2-7 17:04:02 | 显示全部楼层 |阅读模式
现在越来越多的网站都做到可以用户自己选择样色,其原理就是通过用户的选择来切换加载不同的样色表,当然你的页面必须是有比较好的框架、布局等等。下面就以一个比较简单的例子来说明是怎么实现的:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><link rel="stylesheet" href="orange.css" type="text/css" title="orange" /><script language="javascript" type="text/javascript" src="changecss.js"></script></head><body><div class="div-one"><h1 style="margin:0;">NO. 1</h1></div>    <div class="div-two"><h1 style="margin:0;">NO. 2</h1></div><div class="div-three">  <input type="button" value="change color" /></div></body></html>
//changecss.jsfunction changeActiveStyleSheet(){var i,a,href;for(i=0;(a=document.getElementsByTagName('link')[i]);i++)if(a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('title')){if(a.href.indexOf('green')!=-1)href = "orange";else if(a.href.indexOf('orange')!=-1)href = "green";elsehref = "orange";a.href = href + ".css";}
@charset "utf-8";/* orange.css */.div-one{float:left;width:200px;height:200px;opacity = 0.7;background:orange;text-align:center;line-height:200px;border:green 1px dashed;filter:Alpha (opacity = 70);}.div-two{float:left;width:200px;height:200px;background:green;text-align:center;line-height:200px;border: orange 1px dashed;}.div-three{float:left;width:200px;height:200px;text-align:center;}.div-three input{margin:45% 0;}
@charset "utf-8";/* green.css */.div-one{float:left;width:200px;height:200px;opacity = 0.7;background:green;text-align:center;line-height:200px;border: orange 1px dashed;filter:Alpha (opacity = 70);}.div-two{float:left;width:200px;height:200px;    background:orange;text-align:center;line-height:200px;border:green 1px dashed;}.div-three{float:left;width:200px;height:200px;text-align:center;}.div-three input{margin:45% 0;}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表