Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery是否保留触摸事件属性?_Jquery_Iphone_Event Handling_Touch - Fatal编程技术网

jQuery是否保留触摸事件属性?

jQuery是否保留触摸事件属性?,jquery,iphone,event-handling,touch,Jquery,Iphone,Event Handling,Touch,当触摸屏幕时,此代码产生预期的“1”行为: document.getElementById('someNodeId').addEventListener('touchmove', touch, true); function touch(evt) { evt.preventDefault(); alert(evt.changedTouches.length); } 使用jQuery选择器的代码相同: $('#someNodeId').bind('touchmove',

当触摸屏幕时,此代码产生预期的“1”行为:

document.getElementById('someNodeId').addEventListener('touchmove', touch, true);

function touch(evt) {
  evt.preventDefault();
  alert(evt.changedTouches.length);     
  }
使用jQuery选择器的代码相同:

 $('#someNodeId').bind('touchmove', touch);
生成错误:“TypeError:表达式'evt.changedTouches'[undefined]的结果不是对象”

(设备=iPodtouch操作系统3.1.3(7E18);jQuery 1.4.2)

这怎么可能?我做错了什么?

试试看

$(document).ready (function () {
    $("#someNodeId").bind("touchmove", function (event) {
        var e = event.originalEvent;
        console.log(e.targetTouches[0].pageX);
    });
});

我将此简单函数用于基于JQuery的项目

var pointerEventToXY = function(e){
  var out = {x:0, y:0};
  if(e.type == 'touchstart' || e.type == 'touchmove' || e.type == 'touchend' || e.type == 'touchcancel'){
    var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
    out.x = touch.pageX;
    out.y = touch.pageY;
  } else if (e.type == 'mousedown' || e.type == 'mouseup' || e.type == 'mousemove' || e.type == 'mouseover'|| e.type=='mouseout' || e.type=='mouseenter' || e.type=='mouseleave') {
    out.x = e.pageX;
    out.y = e.pageY;
  }
  return out;
};
例如:

$('a').on('mousemove touchmove', function(e){
   console.log(pointerEventToXY(e)); // will return obj ..kind of {x:20,y:40}
})

希望这对您有用;)

解释一下为什么需要使用
originalEvent
,对于OP和其他不熟悉jQuery事件规范化的人来说是有帮助的;这里的解释是:“为了‘修复’事件,jQuery克隆了事件。这样做,出于性能原因,它只复制了有限数量的属性。但是,您仍然可以通过event.originalEvent属性访问原始事件对象。”事实上,我不确定在iPhone上使用jQuery是否非常有用;我走这条路是因为jQTouch,但现在我要离开它。