055-inline-events.html 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>055 inline event attributes</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>055 &mdash; inline event attributes</h1>
  15. <p><code>onclick</code> must run with the element as <code>this</code>, bubble
  16. to an ancestor handler, and <code>return false</code> must prevent the
  17. default action.</p>
  18. <button id="button" onclick="this.setAttribute(&quot;data-x&quot;, &quot;1&quot;); this.textContent = 'clicked'; window.__inline = (window.__inline || 0) + 1;">press me</button>
  19. <div id="parent" onclick="window.__bubbled = (window.__bubbled || 0) + 1;">
  20. <span id="child">bubble source</span>
  21. </div>
  22. <a id="cancel" href="#somewhere" onclick="return false;">cancel link</a>
  23. <div id="result">checking...</div>
  24. <script>
  25. setTimeout(function () {
  26. var b = document.getElementById('button');
  27. var p = document.getElementById('parent');
  28. var c = document.getElementById('child');
  29. var a = document.getElementById('cancel');
  30. b.dispatchEvent(new MouseEvent('click', { bubbles: true }));
  31. c.dispatchEvent(new MouseEvent('click', { bubbles: true }));
  32. var notCancelled = a.dispatchEvent(new MouseEvent('click', {
  33. bubbles: true, cancelable: true
  34. }));
  35. var ok = b.textContent === 'clicked' &&
  36. b.getAttribute('data-x') === '1' &&
  37. window.__inline === 1 &&
  38. window.__bubbled === 1 &&
  39. notCancelled === false;
  40. var r = document.getElementById('result');
  41. if (r) {
  42. r.style.background = ok ? '#c8f0c8' : '#ffc8c8';
  43. r.textContent = (ok ? 'INLINE-OK ' : 'INLINE-FAIL ') +
  44. 'text=' + b.textContent +
  45. ' x=' + b.getAttribute('data-x') +
  46. ' inline=' + window.__inline +
  47. ' bubble=' + window.__bubbled +
  48. ' dispatch=' + notCancelled;
  49. }
  50. }, 300);
  51. </script>
  52. </body>
  53. </html>