最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why can't my onclick handler find its parent node? - Stack Overflow

programmeradmin7浏览0评论

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>

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>
Share Improve this question asked Feb 19, 2009 at 22:13 mikemike 49.2k45 gold badges104 silver badges117 bronze badges 4
  • 1 That's got to be the fastest 9 responses ever! – Russ Cam Commented Feb 19, 2009 at 22:19
  • Obviously I posted before I saw Paolo's response e up. Why 7 other people would post the same answer shortly after... I dunno. – Sean Bright Commented Feb 19, 2009 at 22:21
  • I didn't get the New answers alert while I was writing my answer – Russ Cam Commented Feb 19, 2009 at 22:23
  • The SO heartbeat thing that checks for new answers is flaky these days. – Crescent Fresh Commented Feb 19, 2009 at 22:26
Add a ment  | 

7 Answers 7

Reset to default 7

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

parentNode is a property, not a function.

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 isn't a function, it's a property.

发布评论

评论列表(0)

  1. 暂无评论