diff options
Diffstat (limited to 'files/zh-tw/web/api/customevent/customevent/index.html')
-rw-r--r-- | files/zh-tw/web/api/customevent/customevent/index.html | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/files/zh-tw/web/api/customevent/customevent/index.html b/files/zh-tw/web/api/customevent/customevent/index.html new file mode 100644 index 0000000000..0accf247e5 --- /dev/null +++ b/files/zh-tw/web/api/customevent/customevent/index.html @@ -0,0 +1,90 @@ +--- +title: CustomEvent() +slug: Web/API/CustomEvent/CustomEvent +translation_of: Web/API/CustomEvent/CustomEvent +--- +<p>{{APIRef("DOM")}}</p> + +<p><code><strong>CustomEvent()</strong></code> constructor 可用來建立 {{domxref("CustomEvent")}}.</p> + +<h2 id="語法">語法</h2> + +<pre class="syntaxbox"> <em>event</em> = new CustomEvent(<em>typeArg</em>, <em>customEventInit</em>);</pre> + +<h3 id="參數">參數</h3> + +<dl> + <dt><em>typeArg</em></dt> + <dd>一個 {{domxref("DOMString")}} 用來表示事件名稱。</dd> + <dt><em>customEventInit</em>{{optional_inline}}</dt> + <dd>Is a <code>CustomEventInit</code> dictionary, having the following fields: + <ul> + <li><code>"detail"</code>, optional and defaulting to <code>null</code>, of type any, that is a event-dependant value associated with the event.</li> + </ul> + + <div class="note"> + <p><em>The <code>CustomEventInit</code></em><em> dictionary also accepts fields from the {{domxref("Event.Event", "EventInit")}} dictionary.</em></p> + </div> + </dd> +</dl> + +<h2 id="範例">範例</h2> + +<pre class="brush: js">// add an appropriate event listener +obj.addEventListener("cat", function(e) { process(e.detail) }); + +// create and dispatch the event +var event = new CustomEvent("cat", { + detail: { + hazcheeseburger: true + } +}); +obj.dispatchEvent(event);</pre> + +<h2 id="規格">規格</h2> + +<table class="standard-table" style="height: 49px; width: 1000px;"> + <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','#interface-customevent','CustomEvent()')}}</td> + <td>{{Spec2('DOM WHATWG')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="瀏覽器支援度">瀏覽器支援度</h2> + + + +<p>{{Compat("api.CustomEvent.CustomEvent")}}</p> + +<h2 id="添加額外參數">添加額外參數</h2> + +<p>在 Internet Explorer 9 或更高的版本,你可以用以下的方法給 <code>CustomEvent()</code> constructor 添加額外參數</p> + +<pre class="brush: js">(function () { + function CustomEvent ( event, params ) { + params = params || { bubbles: false, cancelable: false, detail: undefined }; + var evt = document.createEvent( 'CustomEvent' ); + evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail ); + return evt; + } + + CustomEvent.prototype = window.Event.prototype; + + window.CustomEvent = CustomEvent; +})();</pre> + +<h2 id="延伸閱讀">延伸閱讀</h2> + +<ul> + <li>{{domxref("CustomEvent")}}</li> +</ul> |