Search

Javascript - stop event bubbling and default action

2014-11-16 4:09 PM
Examples
  1. // Stop bubbling
  2. event.stopPropagation();
  3.  
  4. // Stop default action
  5. event.preventDefault();
Details
  1. /**
  2. * Stop bubbling
  3. * Use stopPropagation to prevent event bubbling.
  4. */
  5. event.stopPropagation();
  6.  
  7. // Feel free to check the JSFiddle of this example at the bottom of this tutorial.
  8. // If there is a html structure like L1 > L2 > L3.
  9. // To prevent the onclick event from firing when a child is clicked:
  10. $("#L1").on("click", function(event){
  11. alert("L1");
  12. });
  13.  
  14. $("#L2").on("click", function(event){
  15. alert("L2");
  16. });
  17.  
  18. $("#L3").on("click", function(event){
  19. event.stopPropagation();
  20. alert("L3");
  21. });
  22.  
  23. // Feel free to check the JSFiddle of this example at the bottom of this tutorial.
  24. // Disable context menu:
  25. $("#L2").on("contextmenu", function(event){
  26. event.stopPropagation();
  27. event.preventDefault();
  28. });
Links
JSFiddle : event.stopPropagation();
JSFiddle : event.preventDefault();

No comments:

Post a Comment