events.html 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <html>
  2. <!--
  3. Copyright 2010 The Closure Library Authors. All Rights Reserved.
  4. Use of this source code is governed by the Apache License, Version 2.0.
  5. See the COPYING file for details.
  6. -->
  7. <head>
  8. <title>Event Test</title>
  9. <script type="text/javascript" src="../base.js"></script>
  10. <script type="text/javascript">
  11. goog.require('goog.events');
  12. goog.require('goog.events.EventType');
  13. </script>
  14. </head>
  15. <body>
  16. <p>
  17. <a id="link1" href="#link1">Link 1</a><br />
  18. <a id="link2" href="#link2">Link 2</a><br />
  19. <a id="link3" href="#link3">Link 3</a><br />
  20. <a id="link4" href="#link4">Link 4</a>
  21. </p>
  22. <p>
  23. <a href="javascript:addListeners()">Listen</a> |
  24. <a href="javascript:removeListeners()">UnListen</a> |
  25. <a href="javascript:void(goog.events.unlisten($('link1'), 'click', one))">Remove One</a> |
  26. <a href="javascript:void(goog.events.unlisten($('link1'), 'click', two))">Remove Two</a> |
  27. <a href="javascript:void(goog.events.unlisten($('link1'), 'click', three))">Remove Three</a> |
  28. </p>
  29. <pre id="info"></pre>
  30. <div id="test1" style="background-color: pink;">
  31. Test 1
  32. <div id="test2" style="background-color: lightblue;">
  33. &nbsp; &nbsp; Test 2
  34. <div id="test3" style="background-color: lightyellow;">
  35. &nbsp; &nbsp; &nbsp; &nbsp; Test 3
  36. </div>
  37. &nbsp; &nbsp; Test 2
  38. </div>
  39. Test 1
  40. </div>
  41. <script type="text/javascript">
  42. function $(el) { return document.getElementById(el); }
  43. function handleEventC(e) {
  44. alert('capture');
  45. $('info').innerHTML = "CAPTURE<br>" + goog.events.expose(e).replace(/\n/g, '<br>');
  46. }
  47. function handleEventB(e) {
  48. alert('bubble');
  49. $('info').innerHTML = "BUBBLE<br>" + goog.events.expose(e).replace(/\n/g, '<br>');
  50. }
  51. function one() {
  52. alert('1');
  53. }
  54. function two() {
  55. alert('2');
  56. }
  57. function three() {
  58. alert('3');
  59. }
  60. function addListeners() {
  61. goog.events.listen($('link1'), 'click', one);
  62. goog.events.listen($('link1'), 'click', two);
  63. goog.events.listen($('link1'), 'click', three);
  64. goog.events.listen($('link2'), 'click', handleEventB);
  65. goog.events.listen($('link3'), 'click', handleEventB);
  66. goog.events.listen($('link4'), 'click', handleEventB);
  67. }
  68. function removeListeners() {
  69. goog.events.unlisten($('link1'), 'click', handleEventB);
  70. goog.events.unlisten($('link2'), 'click', handleEventB);
  71. goog.events.unlisten($('link3'), 'click', handleEventB);
  72. goog.events.unlisten($('link4'), 'click', handleEventB);
  73. }
  74. addListeners();
  75. goog.events.listen($('test1'), goog.events.EventType.CLICK, handleEventC,
  76. true, $('test3'));
  77. goog.events.listen($('test1'), goog.events.EventType.CLICK, handleEventB,
  78. false, $('test3'));
  79. alert('howdy');
  80. </script>
  81. </body>
  82. </html>