19_DOM事件委托.html 742 B

123456789101112131415161718192021222324252627282930
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <div id="box">
  10. <span>a</span>
  11. <span>b</span>
  12. <span>c</span>
  13. </div>
  14. <script>
  15. // var aSpan = document.getElementsByTagName("span");
  16. // for(var i=0;i<aSpan.length;i++){
  17. // aSpan[i].onclick = function(){
  18. // console.log(this.innerText);
  19. // }
  20. // }
  21. var oBox = document.getElementById("box");
  22. oBox.onclick = function(e){
  23. // console.log(this.innerText)
  24. console.log(e.target.innerText)
  25. }
  26. </script>
  27. </body>
  28. </html>