如何以编程方式在Chrome 41中创建TouchEvent? [英] How do I programmatically create a TouchEvent in Chrome 41?

查看:594
本文介绍了如何以编程方式在Chrome 41中创建TouchEvent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为单元测试创​​建一个触摸事件。阅读 https://developer.mozilla.org/en-US/ docs / Web / API / TouchEvent ,我期望我能做到:

  document.createEvent( '的TouchEvent'); 

但是我得到这个错误:


未捕获的DOMException:未能在'Document'上执行'createEvent':提供的事件类型('TouchEvent')无效。

我看到创建和触发触摸事件发生在一个支持触摸的浏览器上?,这似乎也表明createEvent()是要走的路。



我也尝试通过构造函数创建事件,它适用于MouseEvent和WheelEvent:

  new window.TouchEvent()

但我在这里也得到一个错误:


Uncaught TypeError:非法构造函数

我在Firefox 36中尝试过,但是基于 http://caniuse.com/#search=touch ,我并不惊讶地发现:


NotSupportedError:操作不受支持


运行后

  document.createEvent('TouchEvent')

Firefox中没有window.TouchEvent构造函数,这也不足为奇。



任何想法我做错了什么?

解决方案

p>

  document.createEvent('TouchEvent'); 

在您使用实际移动设备或移动仿真时可以使用。

I am trying to create a touch event for a unit test. After reading https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent, I expected that I would be able to do:

document.createEvent('TouchEvent');

But I get this error:

Uncaught DOMException: Failed to execute 'createEvent' on 'Document': The provided event type ('TouchEvent') is invalid.

I saw Creating and firing touch events on a touch enabled browser?, which also seems to indicate that createEvent() is the way to go.

I also tried creating the event via constructor, which works for, say, MouseEvent and WheelEvent:

new window.TouchEvent()

But I get an error here, too:

Uncaught TypeError: Illegal constructor

I tried in Firefox 36, but based on http://caniuse.com/#search=touch, I wasn't surprised to see:

NotSupportedError: Operation is not supported

After running

document.createEvent('TouchEvent')

There is not event a window.TouchEvent constructor in Firefox, which is, again, not surprising.

Any ideas what I am doing wrong?

解决方案

It looks like

document.createEvent('TouchEvent'); 

works if you are using an actual mobile device or mobile emulation.

这篇关于如何以编程方式在Chrome 41中创建TouchEvent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆