2016-03-09 40 views
3

我工作的有關Custom Events任務,並想知道,是有event dataevent.initEvent VS event.initCustomEvent

MDN - The old-fashioned way這些方法除了額外的參數之間的差異采用event.initEventMDN - Cutom Events的填充工具使用event.initCustomEvent來初始化事件。

我已經提到Passing additional arguments in custom Event,但我們甚至可以使用event.details來設置event data

JSFiddle - Event data with initEvent

所以現在的問題是,有使用特定的其中一方的任何好處?

回答

3

從DOM規範:

initCustomEvent(type, bubbles, cancelable, detail)方法 必須調用時,執行這些步驟:

  1. 如果context objectdispatch flag設置,終止這些步驟。
  2. Initializecontext object類型氣泡,和取消
  3. 設置context objectdetail屬性到細節

initEvent(type, bubbles, cancelable)方法被調用時,必須 運行下列步驟操作:

  1. 如果context objectdispatch flag設置,終止這些步驟。
  2. Initializecontext object類型氣泡,和取消

注:由於events有構造initEvent()是 多餘的。但是,它必須支持傳統內容。

因此,除了detail參數之外,它們是相同的。

2

event.initEvent此功能已從Web標準中刪除。儘管一些瀏覽器可能仍然支持它,但它正在被丟棄。不要在舊項目或新項目中使用它。使用它的頁面或Web應用程序可能會隨時中斷。 MDN

+0

但它仍然存在,都在[W3C](https://www.w3。org/TR/dom /#dom-event-initevent)和[WHATWG](https://dom.spec.whatwg.org/#dom-event-initevent)規範。 – Oriol

相關問題