处理draggables/droppables的iPad touch事件(undefined changedTouches)

Dav*_*iov 7 javascript jquery jquery-ui touch-event

我试图让jQuery拖放与iPad touch事件很好地协同工作.我在互联网上找到了这个代码:

function touchHandler(event)
{
    var touches = event.changedTouches,
        first = touches[0],
        type = "";
    switch (event.type)
    {
        case "touchstart": type = "mousedown"; break;
        case "touchmove": type = "mousemove"; break;
        case "touchend": type = "mouseup"; break;
        default: return;
    }

    //initMouseEvent(type, canBubble, cancelable, view, clickCount, 
    //           screenX, screenY, clientX, clientY, ctrlKey, 
    //           altKey, shiftKey, metaKey, button, relatedTarget);

    var simulatedEvent = document.createEvent("MouseEvent");
    simulatedEvent.initMouseEvent(type, true, true, window, 1,
                              first.screenX, first.screenY,
                              first.clientX, first.clientY, false,
                              false, false, false, 0/*left*/, null);

    first.target.dispatchEvent(simulatedEvent);
    event.preventDefault();
}
Run Code Online (Sandbox Code Playgroud)

如果我将touchHandler附加到documenttouchstart/move/end,它似乎工作正常,但是在iPad上没有允许原生缩放/滚动,所以我试图将这个处理程序附加到draggables本身.

我看到的问题event.changeTouches是由于某种原因总是未定义的,正如您可以在http://jsfiddle.net/myLj2/1/中看到的那样.我无法想出触摸事件将始终具有未定义changeTouches属性的原因,因此,此代码将无法正常工作.有什么想法吗?

Dav*_*iov 9

我发现jQuery是否保留了触摸事件属性后才发现它?.

事实证明,jQuery事件不会复制changeTouches属性,因此您必须使用jQuery的originalEvent属性或者一起跳过jQuery并使用类似的函数绑定addEventListener