87,907
社区成员
发帖
与我相关
我的任务
分享
<!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>
</head>
<body>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td id="item1"> </td>
</tr>
<tr>
<td id="item2"> </td>
</tr>
<tr>
<td id="item3"> </td>
</tr>
</table>
</body>
</html>
<script language="javascript">
function hightlight(o)
{
o.style.backgroundColor='#999999';
}
function normal(o)
{
o.style.backgroundColor='';
}
function addEvent(obj)
{
//这里就是闭包 参数obj在这个闭包里是相对独立的
obj.attachEvent('onmouseover',function(){hightlight(obj);});
obj.attachEvent('onmouseout',function(){normal(obj);});
}
for(var i=1;i<=3;i++)
addEvent(document.getElementById('item'+i));
</script>
for (var i = 1 ; i < 4;i ++) {
var obj = document.getElementById('item' + i);
obj.attachEvent('onmouseover',
function (o) {
return function (e) { hightlight(o); }
}(obj)
);
obj.attachEvent('onmouseout',
function (o) {
return function (e) { normal(o); }
}(obj)
);
}