code_junkie

how to select only the background in jQuery

Nov 13th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. <body>
  2. <div>
  3. <ul>
  4. <li>Foo</li>
  5. <li>Bar</li>
  6. <li>Baz</li>
  7. </ul>
  8. </div>
  9. </body>
  10.  
  11. $(document).ready(function() {
  12. //hookup events for entire body of document
  13. $('body').bind('click', function(e){
  14. //do clicked body stuff
  15. });
  16. //hookup our divs, returning false to prevent bubbling
  17. $('div').bind('click', function(e){
  18. //do clicked div stuff
  19. return false; //stop event propagation
  20. });
  21. });
  22.  
  23. $(document).ready(function() {
  24. //event delegate
  25. $('body').bind('click', function(e){
  26. var originalElement = e.srcElement;
  27. if(!originalElement){originalElement=e.originalTarget;}
  28. if ($(originalElement).is('div')) {
  29. console.log("A div!");
  30. }
  31. else if ($(originalElement).is('div#inside')) {
  32. console.log("A Specific Div!");
  33. }
  34. else{
  35. console.log("Something else!");
  36. }
  37. });
  38. });
  39.  
  40. <html><head>
  41. <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js"></script>
  42. <script type="text/javascript"><!--
  43. $(document).ready(function() {
  44. $(document).click(function() {
  45. alert('document'); return false;
  46. });
  47. $('#my_div').click(function() {
  48. alert('div'); return false;
  49. });
  50. });
  51. --></script>
  52. <body>
  53. <h1>hello!</h1>
  54. <div id="my_div">this div is special</div>
  55. </body></html>
  56.  
  57. $("html").click(function(event){
  58. event.stopPropagation();
  59. alert('document');
  60. });
  61.  
  62. $("div").click(function(event){
  63. event.stopPropagation();
  64. alert('div');
  65. });
  66.  
  67. $(document:not(div)).click(function() {do()});
  68.  
  69. $("div").click(function() {do2()});
Add Comment
Please, Sign In to add comment