萬盛學電腦網

 萬盛學電腦網 >> 腳本專題 >> javascript >> js點擊更換背景顏色或圖片的實例代碼

js點擊更換背景顏色或圖片的實例代碼

1,按鈕樣式

復制代碼 代碼如下:
<script>
org_Color=document.bgColor.substring(1.10)
</script>
<form>
<input type="button" value="淡黃色背景"onClick="document.bgColor='#feffc6'">
<input type="button" value="淺藍色背景"onClick="document.bgColor='#c6fffe'">
<input type="button" value="粉紅色背景"onClick="document.bgColor='#ffc9c6'">
<input type="button" value="墨綠色背景"onClick="document.bgColor='#508b7d'">
<input type="button" value="天藍色背景"onClick="document.bgColor='#7BC7FF'">
<input type="button" value="米白色背景"onClick="document.bgColor='#f0f0f0'">
</form>


2,下拉樣式

復制代碼 代碼如下:
<selectonChange="document.bgColor=this.options[this.selectedIndex].value">
<option value="#C0C0C0">灰色的
<option value="BLACK">黑的色
<option value="d2c6ff">淡紫藍
<option value="feefc7">太陽黃
<option value="ffd2c6">淡紅橘
<option value="c7fed8">蘋果綠
<option value="80ff80">草原綠
<option value="#C1BC59">橄榄色
<option value="#7BC7FF">天空藍
<option value="#AEDFD3">藍綠色
<option value="#508B7D">墨綠色
<option value="#F0F0F0">米白色
</select>


3,觸碰樣式

復制代碼 代碼如下:
<scriptlanguage="Javascript">
<!--
function backcolor(form){
temp = ""
for (var i = 0; i < 16; i++) {
temp = form.color[i].value
if (form.color[i].checked){ document.bgColor = temp }
}
}

function randombackground(){
document.bgColor = getColor()
}

function getColor(){
currentdate = new Date()
backgroundcolor = currentdate.getSeconds()
if (backgroundcolor > 44)
backgroundcolor = backgroundcolor - 45
else if (backgroundcolor > 29)
backgroundcolor = backgroundcolor - 30
else if (backgroundcolor > 15)
backgroundcolor = backgroundcolor - 16
if (backgroundcolor == 0 )
return "olive";
else if (backgroundcolor == 1 )
return "teal";
else if (backgroundcolor == 2 )
return "red";
else if (backgroundcolor == 3 )
return "blue";
else if (backgroundcolor == 4 )
return "maroon";
else if (backgroundcolor == 5 )
return "navy";
else if (backgroundcolor == 6 )
return "lime";
else if (backgroundcolor == 7 )
return "fuschia";
else if (backgroundcolor == 8 )
return "green";
else if (backgroundcolor == 9 )
return "purple";
else if (backgroundcolor == 10 )
return "gray";
else if (backgroundcolor == 11 )
return "yellow";
else if (backgroundcolor == 12 )
return "aqua";
else if (backgroundcolor == 13 )
return "black";
else if (backgroundcolor == 14 )
return "white";
else if (backgroundcolor == 15 )
return "silver";
}
// -->
</script>
<body onload="document.bgColor='lime'; returntrue;">
<a href="javascript:void(0);"onmouseover="randombackground()"><fontsize="5" face="宋體">碰我改變背景顏色</font></a>

點擊更換背景圖片:

復制代碼 代碼如下:
<div style="float:right;margin-right:20px;">
<ahref="javascript:void(0);"><imgclass="button1"onClick="javascript:document.body.style.background='url(images/bg/1.jpg)'"></a>
<ahref="javascript:void(0);"><imgclass="button2"onClick="javascript:document.body.style.background='url(images/bg/2.jpg)'"></a>
<ahref="javascript:void(0);"><imgclass="button3"onClick="javascript:document.body.style.background='url(images/bg/3.jpg)'"></a>
<ahref="javascript:void(0);"><imgclass="button4"onClick="javascript:document.body.style.background='url(images/bg/4.jpg)'"></a>
<ahref="javascript:void(0);"><imgclass="button5"onClick="javascript:document.body.style.background='url(images/bg/5.jpg)'"></a>
<ahref="javascript:void(0);"><imgclass="button6"onClick="javascript:document.body.style.background='url(images/bg/6.jpg)'"></a>
</div>

IE6不能用的解決方法:

復制代碼 代碼如下:


<imgonClick="javascript:document.body.style.background='url(images/bg/1.jpg)';returnfalse;">

copyright © 萬盛學電腦網 all rights reserved