在 jsdom 命令行界面中运行以进行单元测试。
const event = new window.TouchEvent('touchstart');
TypeError: Illegal constructor
为什么这不起作用? 根据这个MDN列出它应该通过构造函数工作。
这很好用:
const event = new window.MouseEvent('mousemove');
这也有效,但已弃用:
const event = document.creatEvent('touchstart');
最佳答案
@马丁马扎道森
正如您提到的,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
希望对您有所帮助。祝你有美好的一天。
关于javascript - TouchEvent非法构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42543645/