代码改变世界

使用this作为方法的参数

2012-09-08 10:18  hongjiumu  阅读(445)  评论(0编辑  收藏  举报

<table>

<tbody>

  <tr id="test"><td></td></tr>

</tbody>

</table>

 

document.getElementById("test").setAttribute("onclick","Test()");

var Test=function(){

  alert(Object.prototype.toString.call(this));

};

结果显示: [object Window]

 

要想取得tr,正确的做法是:

document.getElementById("test").setAttribute("onclick","Test(this)");

var Test=function(row){

  alert(row.cells.length);

};

结果显示:1

正确了,呵呵.