aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/api/eventtarget
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-tw/web/api/eventtarget')
-rw-r--r--files/zh-tw/web/api/eventtarget/dispatchevent/index.html134
-rw-r--r--files/zh-tw/web/api/eventtarget/index.html177
-rw-r--r--files/zh-tw/web/api/eventtarget/removeeventlistener/index.html274
3 files changed, 585 insertions, 0 deletions
diff --git a/files/zh-tw/web/api/eventtarget/dispatchevent/index.html b/files/zh-tw/web/api/eventtarget/dispatchevent/index.html
new file mode 100644
index 0000000000..8977c22c9e
--- /dev/null
+++ b/files/zh-tw/web/api/eventtarget/dispatchevent/index.html
@@ -0,0 +1,134 @@
+---
+title: EventTarget.dispatchEvent()
+slug: Web/API/EventTarget/dispatchEvent
+translation_of: Web/API/EventTarget/dispatchEvent
+---
+<p>{{APIRef("DOM Events")}}</p>
+
+<p>於此 {{domxref("EventTarget")}} 物件上觸發特定的 {{domxref("Event")}} 物件實體,相當於依照註冊的順序呼叫它的 {{domxref("EventListener")}}。一般事件處理規則(包含捕捉(capturing)和可選的冒泡(bubbling)階段)也適用於用 <code>dispatchEvent()</code> 手動觸發事件。</p>
+
+<h2 id="Syntax" name="Syntax">語法</h2>
+
+<pre class="syntaxbox"><em>cancelled</em> = !<em>target</em>.dispatchEvent(<em>event</em>)
+</pre>
+
+<h3 id="參數">參數</h3>
+
+<ul>
+ <li><code>event</code> 是要被觸發的事件({{domxref("Event")}} 物件)。</li>
+ <li><code>target</code> 會被初始化為事件的 {{domxref("Event", "", "target")}} 屬性,其也影響了要呼叫哪一個事件監聽器。</li>
+</ul>
+
+<h3 id="回傳值">回傳值</h3>
+
+<ul>
+ <li>會在事件完成傳遞(捕捉、命中、冒泡三階段)之後才回傳其值。</li>
+ <li>若事件在傳遞過程當中,事件是可被取消的({{domxref("Event.cancelable")}} 屬性為 <code>true</code>)且曾於一個或一個以上的事件處理器中被執行了該事件的 {{domxref("Event.preventDefault()")}} 方法(且事件須確實已被取消了預設行為),即回傳 <code>false</code>。否則回傳 <code>true</code> 值。</li>
+</ul>
+
+<p>若遇到以下 3 種情況,<code>dispatchEvent </code>會拋出錯誤資訊--  <code>UNSPECIFIED_EVENT_TYPE_ERR</code> :</p>
+
+<ol>
+ <li>執行 <code>dispatchEvent</code> 前並未藉由初始化事件指定事件類型</li>
+ <li>事件類型為 <code>null</code> 。</li>
+ <li>事件類型是個空白字串。</li>
+</ol>
+
+<p>這些異常,處理器會報告「異常未捕獲(uncaught exceptions)」;</p>
+
+<p>事件處理器(event handlers)會在一群呼叫堆(nested callstack)上執行:事件的呼叫方(caller)會先由處理器會阻擋暫停執行,直到事件完成才繼續執行,但是要注意的是,事件若發生異常並不會傳回給呼叫方。</p>
+
+<h2 id="Notes" name="Notes">注意</h2>
+
+<p><code>dispatchEvent</code> 是「建立→初始化→觸發」的最後一步驟。這些步驟是用來觸發事件,讓事件完成。事件有多種建立方式,例如用 {{domxref("​document.createEvent")}} 並用 <a href="/en-US/docs/DOM/event.initEvent" title="DOM/event.initEvent">initEvent</a> 或其他特殊 methods ,像是 <a href="/en-US/docs/DOM/event.initMouseEvent" title="DOM/event.initMouseEvent">initMouseEvent</a> 或 <a href="/en-US/docs/DOM/event.initUIEvent" title="DOM/event.initUIEvent">initUIEvent</a> 來初始化。</p>
+
+<p>詳請可參考《{{domxref("Event")}}》。</p>
+
+<h2 id="Example" name="Example">範例</h2>
+
+<p>請參閱《<a href="/zh-TW/docs/Web/Guide/Events/Creating_and_triggering_events">建立或觸發事件</a>》。</p>
+
+<h2 id="Specification" name="Specification">規格</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{SpecName('DOM WHATWG', '#dom-eventtarget-dispatchevent', 'EventTarget.dispatchEvent()')}}</td>
+ <td>{{ Spec2('DOM WHATWG') }}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('DOM4', '#dom-eventtarget-dispatchevent', 'EventTarget.dispatchEvent()')}}</td>
+ <td>{{ Spec2('DOM4') }}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('DOM2 Events', '#Events-EventTarget-dispatchEvent', 'EventTarget.dispatchEvent()')}}</td>
+ <td>{{ Spec2('DOM2 Events') }}</td>
+ <td>初始定義</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_Compatibility" name="Browser_Compatibility">瀏覽器支援度</h2>
+
+<p>{{ CompatibilityTable() }}</p>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Chrome</th>
+ <th>Edge</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari (WebKit)</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>4</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>2</td>
+ <td>9 [1]</td>
+ <td>9.64 (probably earlier)</td>
+ <td>3.2 (probably earlier)</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Android</th>
+ <th>Edge</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>IE Phone</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{ CompatUnknown() }}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{ CompatUnknown() }}</td>
+ <td>{{ CompatUnknown() }}</td>
+ <td>{{ CompatUnknown() }}</td>
+ <td>{{ CompatUnknown() }}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<p>[1]:早期的 IE 版本只支持 IE 特有的 {{domxref("EventTarget.fireEvent()")}} 方法來觸發事件。</p>
diff --git a/files/zh-tw/web/api/eventtarget/index.html b/files/zh-tw/web/api/eventtarget/index.html
new file mode 100644
index 0000000000..4c10aa133e
--- /dev/null
+++ b/files/zh-tw/web/api/eventtarget/index.html
@@ -0,0 +1,177 @@
+---
+title: EventTarget
+slug: Web/API/EventTarget
+tags:
+ - API
+ - DOM
+ - DOM Events
+ - Interface
+ - NeedsTranslation
+ - TopicStub
+ - 待翻譯
+translation_of: Web/API/EventTarget
+---
+<p>{{ ApiRef("DOM Events") }}</p>
+
+<p><strong><code>EventTarget</code></strong> 介面定義了其實作的物件具有接收事件的能力,也可能擁有處理事件的監聽器。</p>
+
+<p>除了最為常見的 {{domxref("Element")}}、{{domxref("Document")}} 與 {{domxref("Window")}} 繼承或實作了 <code>EventTarget</code> 介面之外,其它的物件還有 {{domxref("XMLHttpRequest")}}、{{domxref("AudioNode")}}、{{domxref("AudioContext")}}⋯等等。</p>
+
+<p>許多 <code>EventTarget</code>(包括 Element、Document 和 Window)除了透過 {{domxref("EventTarget.addEventListener()", "addEventListener()")}} 方法外,還可藉由 {{domxref("Document_Object_Model", "DOM")}} 物件的屬性({{Glossary("property/JavaScript", "property")}})或 <a href="/zh-TW/docs/Web/HTML/Element">HTML 元素</a>屬性({{Glossary("attribute")}})來設定<a href="/zh-TW/docs/Web/Guide/DOM/Events/Event_handlers">事件處理器</a>。</p>
+
+<p>{{InheritanceDiagram}}</p>
+
+<h2 id="方法" name="方法">方法</h2>
+
+<dl>
+ <dt>{{domxref("EventTarget.addEventListener()")}}</dt>
+ <dd>於 <code>EventTarget</code> 物件上註冊指定事件的監聽器。</dd>
+ <dt>{{domxref("EventTarget.removeEventListener()")}}</dt>
+ <dd>移除 <code>EventTarget</code> 物件上的指定事件監聽器。</dd>
+ <dt>{{domxref("EventTarget.dispatchEvent()")}}</dt>
+ <dd>對此 <code>EventTarget</code> 物件派送(dispatch)一個事件物件,也就是於此 <code>EventTarget</code> 物件上觸發一個指定的事件物件實體。</dd>
+</dl>
+
+<h3 id="Mozilla_chrome_code_的額外方法">Mozilla chrome code 的額外方法</h3>
+
+<p>Mozilla extensions for use by JS-implemented event targets to implement on* properties. See also <a href="/docs/Mozilla/WebIDL_bindings">WebIDL bindings</a>.</p>
+
+<ul>
+ <li>void <strong>setEventHandler</strong>(DOMString type, EventHandler handler) {{non-standard_inline}}</li>
+ <li>EventHandler <strong>getEventHandler</strong>(DOMString type) {{non-standard_inline}}</li>
+</ul>
+
+<h2 id="範例">範例</h2>
+
+<h3 id="_Simple_implementation_of_EventTarget" name="_Simple_implementation_of_EventTarget">Simple implementation of EventTarget</h3>
+
+<pre class="brush: js">var EventTarget = function() {
+ this.listeners = {};
+};
+
+EventTarget.prototype.listeners = null;
+EventTarget.prototype.addEventListener = function(type, callback) {
+ if (!(type in this.listeners)) {
+ this.listeners[type] = [];
+ }
+ this.listeners[type].push(callback);
+};
+
+EventTarget.prototype.removeEventListener = function(type, callback) {
+ if (!(type in this.listeners)) {
+ return;
+ }
+ var stack = this.listeners[type];
+ for (var i = 0, l = stack.length; i &lt; l; i++) {
+ if (stack[i] === callback){
+ stack.splice(i, 1);
+ return;
+ }
+ }
+};
+
+EventTarget.prototype.dispatchEvent = function(event) {
+ if (!(event.type in this.listeners)) {
+ return true;
+ }
+ var stack = this.listeners[event.type];
+ event.target = this;
+ for (var i = 0, l = stack.length; i &lt; l; i++) {
+ stack[i].call(this, event);
+ }
+ return !event.defaultPrevented;
+};
+</pre>
+
+<p>{{ EmbedLiveSample('_Simple_implementation_of_EventTarget') }}</p>
+
+<h2 id="Specification" name="Specification">規範</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('DOM WHATWG', '#interface-eventtarget', 'EventTarget')}}</td>
+ <td>{{Spec2('DOM WHATWG')}}</td>
+ <td>No change.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('DOM3 Events', 'DOM3-Events.html#interface-EventTarget', 'EventTarget')}}</td>
+ <td>{{Spec2('DOM3 Events')}}</td>
+ <td>A few parameters are now optional (<code>listener</code>), or accepts the <code>null</code> value (<code>useCapture</code>).</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('DOM2 Events', 'events.html#Events-EventTarget', 'EventTarget')}}</td>
+ <td>{{Spec2('DOM2 Events')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_Compatibility" name="Browser_Compatibility">瀏覽器相容性</h2>
+
+<p>{{CompatibilityTable}}</p>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Chrome</th>
+ <th>Edge</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari (WebKit)</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>1.0</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatGeckoDesktop("1")}}</td>
+ <td>9.0</td>
+ <td>7</td>
+ <td>1.0<sup>[1]</sup></td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Android</th>
+ <th>Edge</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>IE Mobile</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>1.0</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatGeckoMobile("1")}}</td>
+ <td>9.0</td>
+ <td>6.0</td>
+ <td>1.0</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<p>[1] <code>window.EventTarget</code> does not exist.</p>
+
+<h2 id="參見">參見</h2>
+
+<ul>
+ <li><a href="/docs/Web/Reference/Events">Event reference</a> - the events available in the platform.</li>
+ <li><a href="/docs/Web/Guide/DOM/Events">Event developer guide</a></li>
+ <li>{{domxref("Event")}} interface</li>
+</ul>
diff --git a/files/zh-tw/web/api/eventtarget/removeeventlistener/index.html b/files/zh-tw/web/api/eventtarget/removeeventlistener/index.html
new file mode 100644
index 0000000000..136aa0cf58
--- /dev/null
+++ b/files/zh-tw/web/api/eventtarget/removeeventlistener/index.html
@@ -0,0 +1,274 @@
+---
+title: EventTarget.removeEventListener()
+slug: Web/API/EventTarget/removeEventListener
+translation_of: Web/API/EventTarget/removeEventListener
+---
+<p>{{APIRef("DOM Events")}}</p>
+
+<p><strong><code>EventTarget.removeEventListener()</code></strong> 方法可以移除先前由 {{domxref("EventTarget.addEventListener()", "addEventListener()")}} 所註冊的事件監聽器。</p>
+
+<h2 id="語法">語法</h2>
+
+<pre class="syntaxbox"><code><em>target</em>.removeEventListener(<em>type</em>, <em>listener</em>[, <em>options</em>]);
+</code><em>target</em>.removeEventListener(<em>type</em>, <em>listener</em>[, <em>useCapture</em>]);
+</pre>
+
+<h3 id="參數">參數</h3>
+
+<dl>
+ <dt><code>type</code></dt>
+ <dd>A string representing the event type to remove.</dd>
+ <dt><code>listener</code></dt>
+ <dd>The {{domxref("EventListener")}} function to remove from the event target.</dd>
+ <dt>options {{optional_inline}}</dt>
+ <dd>An options object that specifies characteristics about the event listener. The available options are:
+ <ul>
+ <li><code>capture</code>: A {{jsxref("Boolean")}} that indicates that events of this type will be dispatched to the registered <code>listener</code> before being dispatched to any <code>EventTarget</code> beneath it in the DOM tree.  </li>
+ <li><code>passive</code>: A {{jsxref("Boolean")}} indicating that the <code>listener</code> will never call <code>preventDefault()</code>. If it does, the user agent should ignore it and generate a console warning.</li>
+ <li>{{non-standard_inline}}<code> mozSystemGroup</code>: Available only in code running in XBL or in Firefox' chrome, it is a {{jsxref("Boolean")}} defining if the listener is added to the system group.</li>
+ </ul>
+ </dd>
+ <dt><code>useCapture</code> {{optional_inline}}</dt>
+ <dd>Specifies whether the {{domxref("EventListener")}} to be removed is registered as a capturing listener or not. If this parameter is absent, a default value of <code>false</code> is assumed.</dd>
+ <dd>If a listener is registered twice, one with capture and one without, remove each one separately. Removal of a capturing listener does not affect a non-capturing version of the same listener, and vice versa.</dd>
+</dl>
+
+<div class="note"><strong>Note:</strong> <code>useCapture</code> was required in most major browsers' early versions. If broad compatibility is desired, you should always provide the <code>useCapture</code> parameter.</div>
+
+<h3 id="回傳值">回傳值</h3>
+
+<p>無。</p>
+
+<h2 id="備註">備註</h2>
+
+<p>If an {{domxref("EventListener")}} is removed from an {{domxref("EventTarget")}} while it is processing an event, it will not be triggered by the current actions. An {{domxref("EventListener")}} will not be invoked for the event it was registered for after being removed, however it can be reattached.</p>
+
+<p>Calling <code>removeEventListener()</code> with arguments that do not identify any currently registered {{domxref("EventListener")}} on the <code>EventTarget</code> has no effect.</p>
+
+<h2 id="範例">範例</h2>
+
+<p>This example shows how to add a <code>click</code>-based event listener and remove a <code>mouseover</code>-based event listener.</p>
+
+<pre class="brush: js">var body =
+        document.querySelector('body'),
+    clickTarget =
+        document.getElementById('click-target'),
+    mouseOverTarget =
+        document.getElementById('mouse-over-target'),
+    toggle = false;
+
+function makeBackgroundYellow() {
+    'use strict';
+
+    if (toggle) {
+        body.style.backgroundColor = 'white';
+    } else {
+        body.style.backgroundColor = 'yellow';
+    }
+
+    toggle = !toggle;
+}
+
+clickTarget.addEventListener('click',
+    makeBackgroundYellow,
+    false
+);
+
+mouseOverTarget.addEventListener('mouseover', function () {
+    'use strict';
+
+    clickTarget.removeEventListener('click',
+        makeBackgroundYellow,
+        false
+    );
+});
+</pre>
+
+<h2 id="規範">規範</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th>Specification</th>
+ <th>Status</th>
+ <th>Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName("DOM WHATWG", "#dom-eventtarget-removeeventlistener", "EventTarget.removeEventListener()")}}</td>
+ <td>{{Spec2("DOM WHATWG")}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName("DOM4", "#dom-eventtarget-removeeventlistener", "EventTarget.removeEventListener()")}}</td>
+ <td>{{Spec2("DOM4")}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName("DOM2 Events", "#Events-EventTarget-removeEventListener", "EventTarget.removeEventListener()")}}</td>
+ <td>{{Spec2("DOM2 Events")}}</td>
+ <td>Initial definition</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="瀏覽器相容性">瀏覽器相容性</h2>
+
+<p>{{CompatibilityTable}}</p>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Chrome</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari (WebKit)</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>1.0<sup>[1][2]</sup></td>
+ <td>{{CompatGeckoDesktop("1")}}<sup>[3]</sup></td>
+ <td>9.0</td>
+ <td>7<sup>[4]</sup></td>
+ <td>1.0<sup>[1]</sup></td>
+ </tr>
+ <tr>
+ <td><code>useCapture</code> made optional</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>6.0</td>
+ <td>9.0</td>
+ <td>11.60</td>
+ <td>{{CompatVersionUnknown}}</td>
+ </tr>
+ <tr>
+ <td><code>options</code> parameter</td>
+ <td>{{CompatChrome(49.0)}}</td>
+ <td> </td>
+ <td> </td>
+ <td> </td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Android</th>
+ <th>Android Webview</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>IE Mobile</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ <th>Chrome for Android</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>1.0<sup>[1]</sup></td>
+ <td>{{CompatVersionUnknown}}<sup>[2]</sup></td>
+ <td>{{CompatGeckoMobile("1")}}<sup>[3]</sup></td>
+ <td>9.0</td>
+ <td>6.0<sup>[4]</sup></td>
+ <td>1.0<sup>[1]</sup></td>
+ <td>{{CompatVersionUnknown}}<sup>[2]</sup></td>
+ </tr>
+ <tr>
+ <td><code>useCapture</code> made optional</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td> </td>
+ <td> </td>
+ <td> </td>
+ <td> </td>
+ <td>{{CompatVersionUnknown}}</td>
+ </tr>
+ <tr>
+ <td><code>options</code> parameter</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatChrome(49.0)}}</td>
+ <td> </td>
+ <td> </td>
+ <td> </td>
+ <td> </td>
+ <td>{{CompatChrome(49.0)}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<p>[1] Although WebKit explicitly added "<code>[optional]</code>" to the <code>useCapture</code> parameter for Safari 5.1 and Chrome 13, it had been working before the change.</p>
+
+<p>[2] Before Chrome 49, the type and listener parameters were optional.</p>
+
+<p>[2] Prior to Firefox 6, the browser would throw an exception if the <code>useCapture</code> parameter was not explicitly <code>false</code>. Prior to Gecko 9.0 {{geckoRelease("9.0")}}, <code>addEventListener()</code> would throw an exception if the listener parameter was <code>null</code>; now the method returns without error, but without doing anything.</p>
+
+<p>[4] Opera 11.60 made the <code>useCapture</code> parameter optional (<a href="http://my.opera.com/ODIN/blog/2011/09/29/what-s-new-in-opera-development-snapshots-28-september-2011-edition">source</a>).</p>
+
+<p>[5] For backwards compatibility, browsers that support <code>options</code> allow the third parameter to be either <code>options</code> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean" title="The Boolean object is an object wrapper for a boolean value."><code>Boolean</code></a>.</p>
+
+<h2 id="Polyfill_to_support_older_browsers">Polyfill to support older browsers</h2>
+
+<p><code>addEventListener()</code> and <code>removeEventListener()</code> are not present in older browsers. You can work around this by inserting the following code at the beginning of your scripts, allowing the use of <code>addEventListener()</code> and <code>removeEventListener()</code> in implementations that do not natively support it. However, this method will not work on Internet Explorer 7 or earlier, since extending the Element.prototype was not supported until Internet Explorer 8.</p>
+
+<pre class="brush: js">if (!Element.prototype.addEventListener) {
+ var oListeners = {};
+ function runListeners(oEvent) {
+ if (!oEvent) { oEvent = window.event; }
+ for (var iLstId = 0, iElId = 0, oEvtListeners = oListeners[oEvent.type]; iElId &lt; oEvtListeners.aEls.length; iElId++) {
+ if (oEvtListeners.aEls[iElId] === this) {
+ for (iLstId; iLstId &lt; oEvtListeners.aEvts[iElId].length; iLstId++) { oEvtListeners.aEvts[iElId][iLstId].call(this, oEvent); }
+ break;
+ }
+ }
+ }
+ Element.prototype.addEventListener = function (sEventType, fListener /*, useCapture (will be ignored!) */) {
+ if (oListeners.hasOwnProperty(sEventType)) {
+ var oEvtListeners = oListeners[sEventType];
+ for (var nElIdx = -1, iElId = 0; iElId &lt; oEvtListeners.aEls.length; iElId++) {
+ if (oEvtListeners.aEls[iElId] === this) { nElIdx = iElId; break; }
+ }
+ if (nElIdx === -1) {
+ oEvtListeners.aEls.push(this);
+ oEvtListeners.aEvts.push([fListener]);
+ this["on" + sEventType] = runListeners;
+ } else {
+ var aElListeners = oEvtListeners.aEvts[nElIdx];
+ if (this["on" + sEventType] !== runListeners) {
+ aElListeners.splice(0);
+ this["on" + sEventType] = runListeners;
+ }
+ for (var iLstId = 0; iLstId &lt; aElListeners.length; iLstId++) {
+ if (aElListeners[iLstId] === fListener) { return; }
+ }
+ aElListeners.push(fListener);
+ }
+ } else {
+ oListeners[sEventType] = { aEls: [this], aEvts: [ [fListener] ] };
+ this["on" + sEventType] = runListeners;
+ }
+ };
+ Element.prototype.removeEventListener = function (sEventType, fListener /*, useCapture (will be ignored!) */) {
+ if (!oListeners.hasOwnProperty(sEventType)) { return; }
+ var oEvtListeners = oListeners[sEventType];
+ for (var nElIdx = -1, iElId = 0; iElId &lt; oEvtListeners.aEls.length; iElId++) {
+ if (oEvtListeners.aEls[iElId] === this) { nElIdx = iElId; break; }
+ }
+ if (nElIdx === -1) { return; }
+ for (var iLstId = 0, aElListeners = oEvtListeners.aEvts[nElIdx]; iLstId &lt; aElListeners.length; iLstId++) {
+ if (aElListeners[iLstId] === fListener) { aElListeners.splice(iLstId, 1); }
+ }
+ };
+}
+</pre>
+
+<h2 id="參見">參見</h2>
+
+<ul>
+ <li>{{domxref("EventTarget.addEventListener()")}}.</li>
+ <li>{{non-standard_inline}}{{domxref("EventTarget.detachEvent()")}}.</li>
+</ul>