admin管理员组文章数量:1253118
I am trying to fire touch events in my JavaScript to simulate user interactions for the purpose of testing features. I have tried the following snippet:
try {
var targetElement = document.elementFromPoint(55, 155);
console.log(targetElement);
var evt = document.createEvent('UIEvent');
evt.initTouchEvent('touchstart', true, true);
evt.view = window;
evt.altKey = false;
evt.ctrlKey = false;
evt.shiftKey = false;
evt.metaKey = false;
targetElement.dispatchEvent(evt);
} catch (except){
alert(except);
}
The above code throws the exception:
TypeError: Result of expression 'evt.initTouchEvent[undefined]' is not a function.
Can somebody point out what I'm doing wrong?
I am trying to fire touch events in my JavaScript to simulate user interactions for the purpose of testing features. I have tried the following snippet:
try {
var targetElement = document.elementFromPoint(55, 155);
console.log(targetElement);
var evt = document.createEvent('UIEvent');
evt.initTouchEvent('touchstart', true, true);
evt.view = window;
evt.altKey = false;
evt.ctrlKey = false;
evt.shiftKey = false;
evt.metaKey = false;
targetElement.dispatchEvent(evt);
} catch (except){
alert(except);
}
The above code throws the exception:
TypeError: Result of expression 'evt.initTouchEvent[undefined]' is not a function.
Can somebody point out what I'm doing wrong?
Share Improve this question edited Apr 23, 2023 at 10:33 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 19, 2011 at 8:07 prkprk 3381 gold badge5 silver badges10 bronze badges2 Answers
Reset to default 9According to w3c touch spec, TouchEvent
is a subclass of UIEvent. Try creating it like:
var evt = document.createEvent('TouchEvent');
change:
evt.initTouchEvent('touchstart', true, true);
to:
evt.initUIEvent('touchstart', true, true);
worked for me with nightly chrome build
本文标签: javascriptCreating and firing touch events on a touch enabled browserStack Overflow
版权声明:本文标题:javascript - Creating and firing touch events on a touch enabled browser? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740748509a2280389.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论