<html xmlns="http://www.w3.org/1999/xhtml">javascript
<head>css
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />html
<title>網頁煥膚</title>java
<style type="text/css">dom
#mainui
{spa
width:800px;xml
height:600px;htm
margin:auto;ip
font-family:"新宋體";
font-size:15px;
}
#top
{
width:800px;
height:30px;
border:1px #666666 solid;
border-bottom:1px #333333 dotted;
}
#bottom
{
width:800px;
height:669px;
border-top:0px;
border:1px #666666 solid;
}
#btRed,#btGreen,#btBlue
{
width:30px;
height:15px;
border:1px #999999 solid;
}
#btRed
{
background-color:#FF0000;
}
#btGreen
{
background-color:#00FF00;
}
#btBlue
{
background-color:#0000FF;
}
span
{
margin-right:2px;
float:right;
}
</style>
<link id="cssStyle" type="text/css" rel="stylesheet" href=""/>
<script type="text/javascript">
function init()
{
/*當第一次加載網頁的時候,隨機產生一個樣式表使用*/
var skinArray=["red","green","blue"];
var index=Math.floor(Math.random()*skinArray.length);
var cssName=skinArray[index];
//document.getElementById("cssStyle").href=cssName+".css";
document.styleSheets[1].href=cssName+".css";
}
function changeSkin(cssName)
{
//document.getElementById("cssStyle").href=cssName+".css";
document.styleSheets[1].href=cssName+".css";
}
</script>
</head>
<body onload="init()">
<div id="main">
<div id="top">
<span>紅色<input type="button" id="btRed" onclick="changeSkin('red')"/></span>
<span>綠色<input type="button" id="btGreen" onclick="changeSkin('green')"/></span><span>藍色<input type="button" id="btBlue" onclick="changeSkin('blue')" /> </span>
<span>網頁換膚:</span>
</div>
<div id="bottom">
</div>
</div>
</body>
</html>
Blue.css
#bottom { background-color:#0000FF; } |
Green.css
#bottom { background-color:#00FF00; } |
本案例的要點爲,同時爲頁面準備多個皮膚(css),當首次加載頁面的時候隨機添加一個皮膚,而後當點擊對應的皮膚的時候加載對應的樣式。同時<link rel="stylesheet" type="text/css" href=""/>中href=""不可缺乏。