jquery 的 removeClass 在移动设备 (HTML) 上运行速度慢一拍

The removeClass of the jquery works one beat slower on the mobile(HTML) device

这真是一个奇怪的案例。

如果我单击一个元素,div 的 class 会立即消失,PC 和 MAC 都会立即消失。使用 removeClass 很好,我想要。

但是移动设备(iPhone 和 Android)出了点问题。 当我尝试单击一个元素时,class 不会在屏幕上删除。但是控制台打印出 class 消失了。所以我点击任何空白区域,此元素正常显示。

我不知道为什么这个问题会发生在移动设备上? 我该怎么做才能让屏幕显示即时反应?

$(".div").on('click touch', function(){
    var idx = $(this).index(".div");
    if($(this).hasClass("yellow")){
          $(this).removeClass("yellow");
    }
}

你可以尝试 touchstart 而不是 touch

$(".div").on('click touchstart', function(){
   var idx = $(this).index(".div");
   if($(this).hasClass("yellow")){
      $(this).removeClass("yellow");
   }
}

如果它是一个动态元素,你必须触发 body

$("body").on('click touchstart', '.div', function(){
   .....
}

使用触摸启动

$(document).on('click touchstart', function () {

ref document .click function for touch device