我部分修改了jquery.tapend
插件以阻止它在 touchmove 上触发点击事件以及它遇到的一些其他问题。这是代码:
(function($) {
$.fn.tap = function(fn) {
return $(this).click.apply(this, arguments) // listen for the final event
.bind('touchstart.jqtapstart', function waitForTouchEnd(e) {
function click(e) {
cancel(e);
$(this).trigger('click');
}
function move(e) {
clearTimeout(timeout);
$this.unbind('.jqtapend');
}
function cancel(e) {
clearTimeout(timeout);
if ('object' === typeof e && 'function' === typeof e.preventDefault) {
e.preventDefault();
e.stopPropagation();
}
$this.unbind('.jqtapend');
}
var timeout = setTimeout(cancel, 250)
, $this = $(this) // maybe > 1 node
;
$this.bind({
'touchend.jqtapend': click,
'touchmove.jqtapend': move,
'touchleave.jqtapend': cancel
});
});
};
})(jQuery);
出于某种原因,Android 设备上的每个“点击”事件都会触发两次,在 iOS 上很少触发,但它仍然会发生。任何人都可以看到为什么会发生这种情况,或者有一个理论吗?
请注意,出于某些原因,我选择不使用 jquery mobile。