html - How to use mobile touch events with pure javascript? - Stack Overflow

What is the syntax for mobile touch events in javascript? I tried:window.document.body.ontouchstart =

What is the syntax for mobile touch events in javascript? I tried:

window.document.body.ontouchstart = function() { alert(); }

and

window.document.body.touchstart = function() { alert(); }

It doesn't give any error. And nothing happens on touching the webpage. It seems like addEventListener is the way to go. But why doesn't window.document.body.ontouchstart directly work?

What is the syntax for mobile touch events in javascript? I tried:

window.document.body.ontouchstart = function() { alert(); }

and

window.document.body.touchstart = function() { alert(); }

It doesn't give any error. And nothing happens on touching the webpage. It seems like addEventListener is the way to go. But why doesn't window.document.body.ontouchstart directly work?

Share Improve this question edited May 16, 2016 at 11:46 user31782 asked May 16, 2016 at 11:29 user31782user31782 7,60716 gold badges79 silver badges158 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3
var theElement = document.getElementById("theElement");

theElement.addEventListener("touchstart", handlerFunction, false);

function handlerFunction(event) {
alert();
}

Try this code:

function foo(event) {
  alert();
}

var el = document.getElementsByTagName("canvas")[0];
  el.addEventListener("touchstart", foo(), false);

//or 

window.document.body.addEventListener("touchstart", foo(), false);
  • Event listner documentation
  • example jsfiddle here

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745320511a4622422.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信