最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - using ontouchend, disable onclick - Stack Overflow

programmeradmin4浏览0评论

i'm creating A webapp for mobile devices and I am using ontouchend for most but not all 'clicks' The problem is I would like to disable the onclick event on my ontouchend functions. Any ideas? Thanks

i'm creating A webapp for mobile devices and I am using ontouchend for most but not all 'clicks' The problem is I would like to disable the onclick event on my ontouchend functions. Any ideas? Thanks

Share Improve this question asked Feb 10, 2012 at 0:05 helptomouthelptomout 1954 silver badges9 bronze badges 1
  • You must put more research effort in your questions, like code for example. – Gigi Commented Feb 10, 2012 at 1:30
Add a ment  | 

2 Answers 2

Reset to default 4

For mobile devices click event is switched with touchend event. With JQuery you can add multiple events using .on() but if you add both a touch and click event both will fire leading to multiple triggers. It is better to use a switch like below:

var eventType = (/Android|webOS|iPhone|iPad|iPod|BlackBerry|Windows Phone/i.test(navigator.userAgent) ) ? 'touchend' : 'click';

$('#item').on(eventType, function(e) {
    //do something
});

Or I also use this as a general switch for touch devices:

var isTouchDevice = (('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch);
var eventType = (isTouchDevice) ? 'touchend' : 'click';

This is a total guess but I would assume you would need to create an event handler for each click event you want to ignore, then simply have e.preventDefault() in the function where e is the event object.

This would then run both events simulataniously however the click event would simply do nothing and you would be left with only the code from the ontouchend event.

There could very well be a better way to do this, like I say, only a guess!

Hope it helps :-)

Andrew

发布评论

评论列表(0)

  1. 暂无评论