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

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

问题描述

为了遵循最佳实践,我们尝试根据您使用的设备使用正确的 JavaScript/jQuery 事件.例如,我们正在构建一个移动网站,该网站的标签将具有点击或触摸事件.对于 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) {
  //...
}

看看这篇文章:

那里发布的 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天全站免登陆