【问题标题】:Unable to get the properties of touchstart event?无法获取 touchstart 事件的属性?
【发布时间】:2011-05-30 07:24:35
【问题描述】:

当我尝试实现以下代码时:

$('.touchelement').bind({'touchstart':function(e){
    console.info(e.touches.item(0));
}});

它将输出显示为未定义。不仅在 touchstart 中,而且对于所有其他事件,例如 (touchmove, touchend),它都会显示相同的结果。

有没有其他方法可以使用 jQuery 绑定 touchstart 事件。

【问题讨论】:

    标签: javascript jquery ios jquery-events


    【解决方案1】:

    jQuery 对事件对象进行一些处理以使它们在浏览器中保持一致,但它不知道 touches 数组,因此不会将其复制到新的事件对象中。因此,您需要通过原始事件对象 event.originalEvent 来访问它。

    $('.touchelement').bind({'touchstart':function(e){
      console.info(e.originalEvent.touches[0]);
    }});
    

    【讨论】:

    • @Richard M 太棒了!!!!!!!!!天哪,谢谢你教我关于 jQuerys tantarum 的丑陋真相!我在 8 小时半的时间里很难找到触摸的去向。先生,您真的是生活品味!
    【解决方案2】:

    不应该看起来更像这样吗?

    $('.touchelement').bind('touchstart', function(e){
      console.info(e.touches);
    });
    

    【讨论】:

    • 抱歉,我想问一下。您是否在支持touchstart 事件的浏览器中进行测试?您可以使用$.support.touch 测试是否支持触摸事件,它返回一个布尔值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多