2017-03-01 47 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

这不是事件名称的问题,这是构造函数的问题。 –

相关问题