扩展jQuery的.on()以使用移动触摸事件

wil*_*ill 9 javascript mobile jquery touch

我试图使用jQuery移动事件而不使用jQuery mobile的其余部分.

https://github.com/jvduf/jquery-mobile-events/blob/master/jquery.mobile.events.js

该片段可以启用它们,并且可以正常工作,但不能使用.on()事件处理程序.例如:

$('a').on('tap',function(){
    console.log('Hi there!');
});
Run Code Online (Sandbox Code Playgroud)

但它确实与.live()一起使用,但现在已经折旧了.

所以我的问题; 有没有办法扩展.on()功能,以包括点击事件和其他?完整列表如下:

  • touchstart
  • touchmove
  • touchend
  • orientationchange
  • 龙头
  • taphold
  • 刷卡
  • swipeleft
  • swiperight
  • scrollstart
  • scrollstop

谢谢 :)

kar*_*m79 9

但它确实与.live()一起使用,但现在已经折旧了.

所以我认为你想使用事件委托来保留替换元素上的那些事件.这意味着:

$('a').on('tap',function () {
    console.log('Hi there!');
});
Run Code Online (Sandbox Code Playgroud)

需要改为:

$(document).on('tap', 'a', function () {
    console.log('Hi there!');
});
Run Code Online (Sandbox Code Playgroud)

为了使它表现得像 $("a").live("tap", ...