delegate.html 663 B

1234567891011121314151617181920212223242526272829
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Delegate</title>
  6. </head>
  7. <body>
  8. <!-- 1. Write some markup -->
  9. <ul>
  10. <li><button>Item 1</button></li>
  11. <li><button>Item 2</button></li>
  12. <li><button>Item 3</button></li>
  13. <li><button>Item 4</button></li>
  14. <li><button>Item 5</button></li>
  15. </ul>
  16. <!-- 2. Include library -->
  17. <script src="../dist/delegate.js"></script>
  18. <!-- 3. Add event delegation -->
  19. <script>
  20. var ul = document.querySelector('ul');
  21. delegate(ul, 'button', 'click', function(e) {
  22. console.log(e.target);
  23. });
  24. </script>
  25. </body>
  26. </html>