如何使用JS / jQuery检查浏览器的touchstart支持? [英] How to check browser for touchstart support using JS/jQuery?

查看:1144
本文介绍了如何使用JS / jQuery检查浏览器的touchstart支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了遵循最佳实践,我们尝试根据您使用的设备使用正确的JavaScript / jQuery事件。例如,我们正在构建一个移动网站,其中包含一个带有onclick或touch事件的标记。对于iPhone,我们想使用touchstart活动。在将该处理程序绑定到对象之前,我们想测试他们的设备是否支持touchstart。如果没有,那么我们将绑定onclick。

In an attempt to follow best practices, we're trying to use the proper JavaScript/jQuery events according to what device you are using. For example, we're building a mobile site that has an tag that will have an onclick or touch event. In the case of an iPhone, we'd like to use the "touchstart" event. We'd like to test if their device supports "touchstart" before we bind that handler to the object. If it doesn't, then we will bind "onclick" instead.

最好的方法是什么?

推荐答案

您可以通过以下方式检测事件是否受到支持:

You can detect if the event is supported by:

if ('ontouchstart' in document.documentElement) {
  //...
}

看一下这篇文章:

  • Detecting event support without browser sniffing

isEventSupported 在那里发布的功能,非常擅长检测各种各样的事件,并且它是跨浏览器的。

The isEventSupported function published there, is really good at detecting a wide variety of events, and it's cross-browser.

这篇关于如何使用JS / jQuery检查浏览器的touchstart支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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