我遇到了一个非常奇怪的问题。我将 touchstart 事件绑定(bind)到一个元素,并希望检索该事件的 X 和 Y 坐标。全部documentation online 表示这可以通过以下方式之一获得:
e.touches
e.targetTouches
e.changedTouches
但是,从 jQuery 运行时,在 #test
上调用的以下代码会为所有三个假定数组返回 undefined
:
$('#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/)。
我的问题是有人知道解决方法吗,或者 jQuery 是否以某种我不知道的方式规范化事件?我测试的所有 jQuery 版本(1.6+)以及 iOS 模拟器和物理 iDevice 上都存在此问题。
作为上述内容的更新,使用普通 JavaScript(而不是 jQuery)调用它会返回正确的结果。例如,以下代码可以正常运行:
document.getElementById('test').addEventListener('touchstart', checkCoords, false);
function checkCoords(e)
{
alert(e.targetTouches);
alert(e.touches);
alert(e.changedTouches);
}
所以看来这是一个 jQuery 问题,而不是其他问题。你可以在这个jsFiddle中看到上面的工作正常。 .
最佳答案
如described in the documentation ,并非原始事件
的每个属性都被复制到 jQuery 的规范化事件对象。
您仍然可以使用originalEvent对象访问它:
$('#test').bind('touchstart', checkCoords );
function checkCoords(e){
alert(e.originalEvent.targetTouches);
alert(e.originalEvent.touches);
alert(e.originalEvent.changedTouches);
}
关于javascript - JavaScript 中的 touchstart 不再返回 TouchList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14999724/