<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>测试</title>
</head>
<body>
<table width="100%" border="1">
<tr>
<td id=hrong> </td>
</tr>
</table>
<script type="text/javascript">
function aa()
{
alert("nodeName = "+ this.nodeName);
}
document.all.hrong.onclick = aa;
</script>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>测试</title>
</head>
<body>
<table width="100%" border="1">
<tr>
<td id=hrong> </td>
</tr>
</table>
<script type="text/javascript">
function aa()
{
alert("nodeName = "+ this.nodeName);
}
document.all.hrong.onclick = aa;
</script>
</body>
</html>
谢谢,我知道这个可以,不过我不理解我的为什么不行?请你解释一下好吗???
在 onclick="aa()" 这样调用函数aa(),是把这个函数作为 onclick() 方法里的一个语句来执行了, 这里的 this 还是指向函数 aa() 本身. 而hrong.onclick = aa这种写法, onclick 不是调用 aa, 而是把 aa 函数里所有语句全部转给了 onclick ,即中间的 aa 这个环节已经没有了. 后面这种方法还有几种等价的定法:
function document.all.hrong.onclick(){alert("nodeName = "+ this.nodeName);}
function document.all.hrong.onclick = function()
{alert("nodeName = "+ this.nodeName);} //与这个特别象, 可以从这里理解切入点
看你们都答得这么爽,我也来补充一下方法。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>测试</title>
<script type="text/javascript">
function aa(a)
{
alert(a.nodeName);
}
</script>
</head>
<body>
<table width="100%" border="1">
<tr>
<td onClick="aa(this);"> </td>
</tr>
</table>
</body>
</html>
你的好意我心领啦。谢谢!