如何在浏览器上检查真实的触摸支持

如何在浏览器上检查真实的触摸支持

本文介绍了如何在浏览器上检查真实的触摸支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天(或最近)Chrome Beta更新为17,对我而言,我注意到我的网络应用程序有一些乐趣。我注意到这是因为一个类被添加到body元素中,通常只有在触摸事件支持的情况下才会被放在那里,我检查这样:

Today (or very recently) Chrome Beta updated to 17 for me and with it i noticed some funkiness in my web app. I noticed it was because a class was being added to the body element that normally only gets put there if there is touch event support which I check like this:

  try {
    document.createEvent("TouchEvent");
    _device.touch = true;
  } catch (e) {
    _device.touch = false;
  }

确实,我可以在Chrome 17上创建和触发触摸事件。我的想法是,哦,我可以检查触摸,看看是否鼠标点击失败,因此,有一个鼠标,但是MouseEvents也触发。

And sure enough, i can create and trigger touch events on Chrome 17. First idea i had was, oh, i can check for touch, and see if a mouse click fails, therefore, there's a mouse, but MouseEvents trigger too.

我检查,没有用户代理嗅探,它是一个实际的,可触摸的设备,而不仅仅是支持触摸事件的浏览器。

How else can I check, without user agent sniffing, that it's an actual, touchable, device, and not just a browser that supports touch events.

推荐答案

p>尝试:

'ontouchstart' in document.documentElement

这篇关于如何在浏览器上检查真实的触摸支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 21:51