Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <body>
- <div>
- <ul>
- <li>Foo</li>
- <li>Bar</li>
- <li>Baz</li>
- </ul>
- </div>
- </body>
- $(document).ready(function() {
- //hookup events for entire body of document
- $('body').bind('click', function(e){
- //do clicked body stuff
- });
- //hookup our divs, returning false to prevent bubbling
- $('div').bind('click', function(e){
- //do clicked div stuff
- return false; //stop event propagation
- });
- });
- $(document).ready(function() {
- //event delegate
- $('body').bind('click', function(e){
- var originalElement = e.srcElement;
- if(!originalElement){originalElement=e.originalTarget;}
- if ($(originalElement).is('div')) {
- console.log("A div!");
- }
- else if ($(originalElement).is('div#inside')) {
- console.log("A Specific Div!");
- }
- else{
- console.log("Something else!");
- }
- });
- });
- <html><head>
- <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js"></script>
- <script type="text/javascript"><!--
- $(document).ready(function() {
- $(document).click(function() {
- alert('document'); return false;
- });
- $('#my_div').click(function() {
- alert('div'); return false;
- });
- });
- --></script>
- <body>
- <h1>hello!</h1>
- <div id="my_div">this div is special</div>
- </body></html>
- $("html").click(function(event){
- event.stopPropagation();
- alert('document');
- });
- $("div").click(function(event){
- event.stopPropagation();
- alert('div');
- });
- $(document:not(div)).click(function() {do()});
- $("div").click(function() {do2()});
Add Comment
Please, Sign In to add comment