哪个元素被点击了?

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function whichElement(e)
  5. {
  6. var targ
  7. if (!e) var e = window.event
  8. if (e.target) targ = e.target
  9. else if (e.srcElement) targ = e.srcElement
  10. if (targ.nodeType == 3) // defeat Safari bug
  11. targ = targ.parentNode
  12. var tname
  13. tname=targ.tagName
  14. alert("You clicked on a " + tname + " element.")
  15. }
  16. </script>
  17. </head>
  18.  
  19. <body onmousedown="whichElement(event)">
  20. <p>在文档中点击某个位置。消息框会提示出您所点击的标签的名称。</p>
  21.  
  22. <h3>这是标题</h3>
  23. <p>这是段落。</p>
  24. <img src="/i/eg_mouse2.jpg" />
  25. </body>
  26.  
  27. </html>