js动态创建表格、鼠标上移改变背景

<!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=gb2312" />
<title>改变背景</title>
<script type="text/javascript">
var current="";
function Over(num)
{
 //alert(num);
 current=document.getElementById("id"+num).style.backgroundColor;
 //alert(current);
 document.getElementById("id"+num).style.backgroundColor="#006699";
}
function Out(num)
{
 document.getElementById("id"+num).style.backgroundColor=current;
}
//创建表格
function Init()
{
 var str="<table border=1 style='border-collapse:collapse'>";
 for(var i=0;i<5;i++)
 {
  str+="<tr id='id"+i+"' style='background-color:#99FF66' onmouseover=Over("+i+") onmouseout=Out("+i+")><td>通过事件变换图片</td>";
  str=str+"</tr>";
 }
 str=str+"</table>";
 div1.innerHTML=str;
 //document.write(eval(str));
}
</script>
</head>

<body onload="Init()">
<div id="div1">
</div>
</body>
</html>

posted @ 2009-10-27 14:38  坐看风起  阅读(676)  评论(0编辑  收藏  举报