简体   繁体   中英

Why can't my onclick handler find its parent node?

Why does the onclick handler below trigger an "elem.parentNode is not a function" error?

<html>
  <head>
   <script type="text/javascript">
     function getParent(elem) {
       var parent = elem.parentNode();
     }
   </script>
  </head>

  <body>
    <div style="border: solid black 2px">
      <span onclick="getParent(this)">hello</span>
    </div>
  </body>
</html>

Your problem is that parentNode is not a function. Try removing the () .

parentNode是一个属性,而不是一个函数。

var parent = element.parentNode;

Because parentNode is not a function? Try elem.parentNode without the parenthesis.

it should be

 function getParent(elem) {
   var parent = elem.parentNode;
}

It's not a function. It's a property. Lose the parentheses.

var parent = elem.parentNode;

parentNode is a property not a function. Drop the () and it should work.

parentNode不是一个函数,它是一个属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM