<html>
<head>
<script>
function test(obj) {
    alert(this.parentNode)    
}
</script>
</head>
<body>
<table>
<tr>
<td><input value="sss" onclick=test(this)></input></td>
</tr>
</body>
</html>
求助,为什么上面的td框点击获取不到父节点?jsfunction

解决方案 »

  1.   


    function test(obj) {
        alert(obj.parentNode);    
    }
      

  2.   


    <!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=utf-8" />
    <title>无标题文档</title>
    <script type="text/javascript">
    function test(obj){
    alert(obj.parentNode.tagName);
    }
    </script>
    </head><body>
    <table>
    <tr>
    <td><input type="button" value="sss" onclick="test(this)" /></td>
    </tr>
    </table>
    </body>
    </html>
      

  3.   

    <table>
    <tr>
        <td>
          <div>
          <input id='test' value="sss" onclick="test(this)" />
          </div>
        </td>
    </tr>
    function test(obj) {
        obj = document.getElementById('test');
        //alert(obj);
        alert(obj.parentNode);
    }
     test();
    this指向你要传进来的那个参数
      

  4.   

    <table>
    <tr>
        <td>
          <div>
    <input id='test' value="sss" type="text" onclick="test(this)" />
          </div>
        </td>
    </tr>
    </table>
    function test(obj) {   
        alert(obj.parentNode);
    }
    函数里this.parentNode,这里的this指向的是window