扩展jQuery的.on()以使用移动触摸事件 [英] Extend jQuery's .on() to work with mobile touch events
问题描述
我正在尝试使用jQuery移动事件,而不使用其余的jQuery mobile。
I am attempting to use the jQuery mobile events without the rest of jQuery mobile.
https://github.com/jvduf/jquery-mobile-events/blob/master/jquery.mobile.events.js
该片段可以全部启用它们,并且工作正常,但不能使用.on()事件处理程序。例如:
That snippet enables them all, and works fine, but not with the .on() event handler. E.g:
$('a').on('tap',function(){
console.log('Hi there!');
});
然而它确实适用于.live(),但现在已经折旧了。
However it does work with .live(), but that is now depreciated.
所以我的问题;有没有办法扩展.on()功能以包括tap事件和其他?完整列表如下:
So my question; is there a a way to extend the .on() functionality to include the tap event and others? Full list below:
- touchstart
- touchmove
- touchend
- orientationchange
- 点按
- taphold
- swipe
- swipeleft
- swiperight
- scrollstart
- scrollstop
- touchstart
- touchmove
- touchend
- orientationchange
- tap
- taphold
- swipe
- swipeleft
- swiperight
- scrollstart
- scrollstop
谢谢:)
推荐答案
然而它确实与.live()一起使用,但现在已经折旧了。
However it does work with .live(), but that is now depreciated.
所以我认为你想要使用事件委托来保留替换元素上的那些事件。这意味着:
So I take it that you want to use event delegation to preserve those events on replaced elements. That would mean that this:
$('a').on('tap',function () {
console.log('Hi there!');
});
需要更改为:
$(document).on('tap', 'a', function () {
console.log('Hi there!');
});
为了使其行为与 $(a)相同.live(点击,...
这篇关于扩展jQuery的.on()以使用移动触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!