054-event-constructors.html 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>054 CustomEvent and MouseEvent</title>
  6. <style>
  7. body { background: #ffffff; color: #202020; font-family: sans-serif;
  8. margin: 12px; font-size: 18px; }
  9. #result { background: #ffc8c8; color: #000000; padding: 8px;
  10. margin-top: 12px; }
  11. </style>
  12. </head>
  13. <body>
  14. <h1>054 &mdash; CustomEvent and MouseEvent</h1>
  15. <p>The constructors must carry <code>detail</code> and the mouse fields into
  16. <code>dispatchEvent</code>, and <code>bubbles</code> must reach the parent.</p>
  17. <div id="parent"><button id="child">event target</button></div>
  18. <div id="result">checking...</div>
  19. <script>
  20. setTimeout(function () {
  21. var parent = document.getElementById('parent');
  22. var child = document.getElementById('child');
  23. var got = [];
  24. parent.addEventListener('custom', function (e) {
  25. got.push('parent:' + e.detail);
  26. });
  27. child.addEventListener('custom', function (e) {
  28. got.push('child:' + e.detail);
  29. });
  30. child.dispatchEvent(new CustomEvent('custom', {
  31. detail: 42, bubbles: true
  32. }));
  33. child.addEventListener('mouse', function (e) {
  34. got.push('mouse:' + e.clientX + ',' + e.clientY + ',' + e.button);
  35. });
  36. child.dispatchEvent(new MouseEvent('mouse', {
  37. bubbles: true, clientX: 12, clientY: 34, button: 2
  38. }));
  39. child.addEventListener('cancel', function (e) {
  40. e.preventDefault();
  41. });
  42. var notCancelled = child.dispatchEvent(new CustomEvent('cancel', {
  43. cancelable: true
  44. }));
  45. var ok = got.join('|') === 'child:42|parent:42|mouse:12,34,2' &&
  46. notCancelled === false;
  47. var r = document.getElementById('result');
  48. if (r) {
  49. r.style.background = ok ? '#c8f0c8' : '#ffc8c8';
  50. r.textContent = (ok ? 'EVENTS-OK ' : 'EVENTS-FAIL ') +
  51. got.join('|') + ' dispatch=' + notCancelled;
  52. }
  53. }, 300);
  54. </script>
  55. </body>
  56. </html>