Javascript网页换肤

效果图:

代码:

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 5 <title>JS网页换肤</title>
 6 <style type="text/css">
 7     html,body,div,ul,li,h1{padding: 0;margin: 0;}
 8     li{list-style: none;}
 9     a{text-decoration: none;color: #ccc;font-size: 12px;}
10     #colorBtn{width: 960px;margin: 10px auto;overflow: hidden;}
11     #colorBtn li{float: left;margin-right: 10px; width: 14px;height: 14px;}
12     .green{background: green;}
13     .red{background: red;}
14     #backChange{width: 960px;margin: 20px auto 0 auto;border-radius: 4px;}
15     #backChange h1{font-size: 14px;height: 24px;line-height: 24px;padding: 0 5px;}
16     .content{width: 948px;padding: 5px;height: 100px;font-size: 14px;}
17 </style>
18 <link id="styleChange" rel="stylesheet" href="styleGreen.css" />
19 <!-- 样式  end -->
20 <script type="text/javascript">
21     var schange=document.getElementById('styleChange'); //全局变量 不写在函数之内 可以影响toGreen()和toRed()
22     function toGreen(){
23         schange.href='styleGreen.css';  //改变链接的样式表
24     }
25     function toRed(){
26         schange.href='styleRed.css';
27     }
28 </script>
29 <!-- JS网页换肤 end -->
30 </head>
31 <body>
32 <ul id="colorBtn" >
33     <li class="green" onclick="toGreen()"></li>
34     <li class="red" onclick="toRed()"></li>
35 </ul>
36 <!-- 颜色按钮 end -->
37 <div id="backChange" >
38     <h1>标题</h1>
39     <div class="content">
40         内容
41     </div>
42 </div>
43 </body>
44 </html>
styleGreen.css
1 @charset "utf-8";
2 /* CSS Document */
3 
4 #backChange{border:1px solid green;}
5 #backChange h1{color:green;border-bottom: 1px solid green;}
6 .content{color: green;}
styleRed.css
1 @charset "utf-8";
2 /* CSS Document */
3 
4 #backChange{border:1px solid red;}
5 #backChange h1{color:red;border-bottom: 1px solid red;}
6 .content{color: red;}

 

知识点:

1、原理:通过onclick使页面加载不同的css样式

2、*页面内任何标签都可以添加ID

3、*页面内任何标签的任何属性都可以通过JS更改。且html怎么写,js就怎么改(className例外,因为class为JS保留字)。

4、实例中的schange为全局变量,详细参考:http://www.cnblogs.com/xianbing/archive/2012/02/27/2369369.html

 

转载于:https://www.cnblogs.com/rm1314/p/3493555.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值