Created
January 22, 2017 14:45
-
-
Save solominh/79d23cab329c6bfb48ee1e6d3bb3cde5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html lang="en"> | |
| <head> | |
| <meta http-equiv="Content-Type" content="text/html;charset:utf-8"> | |
| <title>Event Delegate</title> | |
| </head> | |
| <body> | |
| <ul> | |
| <li><a href="#">item 1</a></li> | |
| <li><a href="#">item 2</a></li> | |
| <li><a href="#">item 3</a></li> | |
| <li><a href="#">item 4</a></li> | |
| </ul> | |
| <a href="#" id='add-item' >Add item</a> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script> | |
| <script> | |
| // Add-item button | |
| $('#add-item').click(function(){ | |
| $('ul li:last-child').clone().appendTo('ul'); | |
| return false; | |
| }); | |
| // Not woking on newly-added items | |
| // $('a').not('#add-item').click(function(){ | |
| // alert('Clicked'); | |
| // return false; | |
| // }); | |
| // Use event delegate | |
| $('ul').click(function(e){ | |
| console.dir(e); | |
| if ($(e.target).is('a')){ | |
| alert('Clicked'); | |
| } | |
| }); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment