2017-03-01 46 views
2

在單元測試的jsdom命令行界面中運行。TouchEvent非法構造函數

const event = new window.TouchEvent('touchstart'); 

TypeError: Illegal constructor

這是爲什麼不工作? 根據這個MDN列表它應該通過構造函數工作。

這工作得很好:

const event = new window.MouseEvent('mousemove'); 

這也適用,但不建議:像你提到

const event = document.creatEvent('touchstart'); 

回答

0

@馬丁馬紮道森

的createEvent方法已被棄用。只要傳入方法的事件在列表中,您仍然可以使用document.createEvent。以下是您可以傳入createEvent方法的事件類型列表。 https://developer.mozilla.org/en-US/docs/Web/API/Document/createEvent

'touchstart'事件類型在給出的列表中不存在。我相信你指的是'ontouchstart',它是一種可以在DOM中存在的單個元素上執行的方法。 ontouchstart的文檔可以在這裏找到。 https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/ontouchstart

我希望這可以幫助你。祝你有美好的一天。

+0

這不是事件名稱的問題,這是構造函數的問題。 –

相關問題