js中变色小特效

来源:互联网 发布:婚庆策划软件 编辑:程序博客网 时间:2024/04/29 10:11

<!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>
<style>
#div1{
 background:#FF3;
 width:200px;
 height:200px;
}
</style>
<script>
function toGreen(){ //定义一个函数 变绿 
 var chan=document.getElementById('div1');    //定义变量改变    获取div1
 chan.style.background='green';    //变量的风格的背景变为绿色
}
function setColor(color){    //定义函数   设定颜色
 var oDiv=document.getElementById('div1');
 oDiv.style.background=color;//不用单引号或者双信号
}

</script>
</head>

<body>

<input type="button" value="变绿" onclick="toGreen()">   //一些按钮
<input type="button" value="变红" onclick="setColor('red')">
 <input type="button" value="变黑" onclick="setColor('black’)"><--不能使用双引号否则就会出现SyntaxError: expected expression, got end of script-->
<div id="div1">
</div>
</body>
</html>

0 0