JavaScript中的touchstart不再返回TouchList [英] touchstart in JavaScript no longer returns TouchList

查看:275
本文介绍了JavaScript中的touchstart不再返回TouchList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常奇怪的问题。我将 touchstart 事件绑定到一个元素,并希望检索事件的X和Y坐标。所有在线文档都表明这一点可通过以下方式之一获得:

I ran into a very strange problem. I am binding the touchstart event to an element, and would like to retrieve the X and Y co-ordinates of the event. All documentation online indicates that this is available through one of the following:


  • e.touches

  • e.targetTouches

  • e.changedTouches

  • e.touches
  • e.targetTouches
  • e.changedTouches

但是,以下代码在 #test 上调用,返回 undefined 从jQuery运行时所有三个假设的数组:

However, the following code, called on #test returns undefined for all three supposed arrays when running this from jQuery:

$('#test').bind('touchstart', function(e) {
    alert(e.targetTouches);
    alert(e.touches);
    alert(e.changedTouches);
});

你可以在这里看到一个演示> http://jsfiddle.net/c7UKV/1/ (您需要在移动设备上查看它,或者您可以直接在此处查看结果> http://jsfiddle.net/c7UKV/1/embedded/result/ )。

You can see a demo here > http://jsfiddle.net/c7UKV/1/ (you need to view it on a mobile device, or you can go straight to the results here > http://jsfiddle.net/c7UKV/1/embedded/result/).

我的问题是有没有人知道一个解决方法,或者是jQuery以某种我不知道的方式规范事件?我测试的所有版本的jQuery(1.6+)以及iOS模拟器和物理iDevices上都存在这个问题。

My question is does anyone know of a work around, or is jQuery normalizing the event in some way that I'm not aware of? The issue exists across all versions of jQuery I tested (1.6+) and also on the iOS Simulator and on physical iDevices.

作为对上述内容的更新,请使用vanilla JavaScript(而不是jQuery)返回正确的结果。例如,下面的代码没有问题:

As an update to the above, calling this using vanilla JavaScript (rather than jQuery) returns the correct result. For example the following code works without a problem:

document.getElementById('test').addEventListener('touchstart', checkCoords, false);

function checkCoords(e)
{
    alert(e.targetTouches);
    alert(e.touches);
    alert(e.changedTouches);
}

因此看起来这是一个jQuery问题,而不是其他任何问题。您可以在 jsFiddle 中看到上述内容正常工作。

So it would appear that this is a jQuery issue, rather than anything else. You can see the above working correctly in this jsFiddle.

推荐答案

as 文档中描述的,并非原始事件的每个属性都被复制到jQuery的规范化事件对象。

As described in the documentation, not every property of the original event is copied to jQuery's normalized event object.

你仍然可以使用originalEvent对象访问它:

You can still access it using originalEvent object:

$('#test').bind('touchstart', checkCoords );

function checkCoords(e){
  alert(e.originalEvent.targetTouches);
  alert(e.originalEvent.touches);
  alert(e.originalEvent.changedTouches);
}

这篇关于JavaScript中的touchstart不再返回TouchList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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