在jquery中如何区分触摸和点击?

在jquery中如何区分触摸和点击?,jquery,Jquery,我正在使用一个运行在WindowsPC和iPad上的jquery。但在ipad上,它会点击两次。 代码是: $("#texthighlight2").on('click,tap,touch',function(){ }) 用这个 $('#texthighlight2').on('click tap', function() { ... }); 用这个 $('#texthighlight2').on('click tap', function() { ... }); 如果有

我正在使用一个运行在WindowsPC和iPad上的jquery。但在ipad上,它会点击两次。 代码是:

$("#texthighlight2").on('click,tap,touch',function(){

})
用这个

$('#texthighlight2').on('click tap', function() {
    ...
});
用这个

$('#texthighlight2').on('click tap', function() {
    ...
});

如果有轻触或触摸,则忽略单击:

var ignoreClick = false;
$("#texthighlight2").on('click tap touch', function(e){
  switch (e.type) {
    case 'tap':
    case 'touch': ignoreClick = true; break;
    case 'click': if (ignoreClick) return;
  }
  // ...
});

作为插件可能更有用:

$.fn.touch = function(handler){
  var ignoreClick = false;
  return this.on('click tap touch', function(e){
    switch (e.type) {
      case 'tap':
      case 'touch': ignoreClick = true; break;
      case 'click': if (ignoreClick) return;
    }
    handler.call(this, e);
  });
};
用法:

$('#texthighlight2').touch(function(){
  // ...
});

如果有轻触或触摸,则忽略单击:

var ignoreClick = false;
$("#texthighlight2").on('click tap touch', function(e){
  switch (e.type) {
    case 'tap':
    case 'touch': ignoreClick = true; break;
    case 'click': if (ignoreClick) return;
  }
  // ...
});

作为插件可能更有用:

$.fn.touch = function(handler){
  var ignoreClick = false;
  return this.on('click tap touch', function(e){
    switch (e.type) {
      case 'tap':
      case 'touch': ignoreClick = true; break;
      case 'click': if (ignoreClick) return;
    }
    handler.call(this, e);
  });
};
用法:

$('#texthighlight2').touch(function(){
  // ...
});

on('click tap touch'
此处没有逗号。若要查找引发的是哪一个:
event。键入
on('click tap touch'
此处没有逗号。若要查找引发的是哪一个:
event.type