Javascript 动态切换样色表

现在越来越多的网站都做到可以用户自己选择样色,其原理就是通过用户的选择来切换加载不同的样色表,当然你的页面必须是有比较好的框架、布局等等。下面就以一个比较简单的例子来说明是怎么实现的:

<!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" onclick="changeActiveStyleSheet()"/>
</div>
</body>
</html>


//changecss.js
function 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";
else
href = "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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值