| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>054 CustomEvent and MouseEvent</title>
- <style>
- body { background: #ffffff; color: #202020; font-family: sans-serif;
- margin: 12px; font-size: 18px; }
- #result { background: #ffc8c8; color: #000000; padding: 8px;
- margin-top: 12px; }
- </style>
- </head>
- <body>
- <h1>054 — CustomEvent and MouseEvent</h1>
- <p>The constructors must carry <code>detail</code> and the mouse fields into
- <code>dispatchEvent</code>, and <code>bubbles</code> must reach the parent.</p>
- <div id="parent"><button id="child">event target</button></div>
- <div id="result">checking...</div>
- <script>
- setTimeout(function () {
- var parent = document.getElementById('parent');
- var child = document.getElementById('child');
- var got = [];
- parent.addEventListener('custom', function (e) {
- got.push('parent:' + e.detail);
- });
- child.addEventListener('custom', function (e) {
- got.push('child:' + e.detail);
- });
- child.dispatchEvent(new CustomEvent('custom', {
- detail: 42, bubbles: true
- }));
- child.addEventListener('mouse', function (e) {
- got.push('mouse:' + e.clientX + ',' + e.clientY + ',' + e.button);
- });
- child.dispatchEvent(new MouseEvent('mouse', {
- bubbles: true, clientX: 12, clientY: 34, button: 2
- }));
- child.addEventListener('cancel', function (e) {
- e.preventDefault();
- });
- var notCancelled = child.dispatchEvent(new CustomEvent('cancel', {
- cancelable: true
- }));
- var ok = got.join('|') === 'child:42|parent:42|mouse:12,34,2' &&
- notCancelled === false;
- var r = document.getElementById('result');
- if (r) {
- r.style.background = ok ? '#c8f0c8' : '#ffc8c8';
- r.textContent = (ok ? 'EVENTS-OK ' : 'EVENTS-FAIL ') +
- got.join('|') + ' dispatch=' + notCancelled;
- }
- }, 300);
- </script>
- </body>
- </html>
|