JS:event.touches属性如何工作? [英] JS: How does event.touches property work?

查看:87
本文介绍了JS:event.touches属性如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白如何使用event.touches属性。例如,要获取iPad / iPhone上的手指数量,您应该使用

I don't understand how to use the event.touches property. For example to get the number of fingers on a iPad/iPhone you should use

event.touches.length

那么为什么这个示例代码不起作用?

Then why is this example code not working?

$('.image').bind('touchstart', function(event) {
  alert(event.touches.length);
});

但这是有效的:

$('.image').bind('touchstart', function() {
  alert(event.touches.length);
});

不应该是反过来的吗?

推荐答案

在第二种情况下,您将回到浏览器的全局(和原始)事件对象而不是特定于jQuery的浏览器(在支持全局事件的浏览器上,并非所有事情都发生— Firefox不支持,例如 —因为它只是其他一些微软的东西已经复制了。)

In the second case, you're falling back on the browser's global (and raw) event object rather than the jQuery-specific one (on browsers that support a global event, not all do — Firefox doesn't, for instance — because it's a Microsoft thing only some others have copied).

听起来jQuery没有在其自定义事件对象中传递 touches 属性。如事件对象的文档中所述,您可以使用 event.originalEvent 在你的第一个例子中访问它所具有的特殊属性,同时仍然获得了jQuery规范化事件对象的好处,例如:

It sounds like jQuery isn't passing the touches property on in its custom event object. As described in the event object's documentation, you can use event.originalEvent in your first example to access the "special properties" it has while still getting the benefits of the jQuery normalized event object, e.g.:

$('.image').bind('touchstart', function(event) {
  alert(event.originalEvent.touches.length);
});

...或者您可以通过在开头执行此操作来告诉jQuery复制该属性你的脚本:

...or you can tell jQuery to copy that property over by doing this once at the beginning of your script:

jQuery.event.props.push("touches");

...因此,您的第一个示例应该开始工作。

...whereupon your first example should start working.

附注:如果你没有使用 jQuery Touch ,你可能会看一下在它。我的猜测是,除了它做的其他事情之外,它还添加了 touches 属性,如上所示(但这只是猜测)。

Side note: If you're not using jQuery Touch, you might look at it. My guess is that in addition to the other things it does, it adds the touches property as shown above (but that's just a guess).

这篇关于JS:event.touches属性如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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