使用this作为方法的参数

<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

正确了,呵呵.

原文地址:https://www.cnblogs.com/hongjiumu/p/2676315.html