简体   繁体   English

jQuery删除和添加类不适用于iPhone

[英]jquery remove and addclass doesn't work on iphone

I have 2 divs. 我有2个div。 One is the label div, the other one is the input div. 一个是标签div,另一个是输入div。 When I toggle the label div, the input div is toggling. 当我切换标签div时,输入div在切换。 I am also closing the input div by clicking anywhere on the screen. 我还要通过单击屏幕上的任意位置来关闭输入div。 Everything works fine, but nothing works on an iPhone. 一切正常,但在iPhone上没有任何效果。

. yoket {
  display: none;
}
$(document).mouseup(function(e) {
  var label = $("#semt");
  var container1 = $("#semt1");

  if (label.has(e.target).length === 1) {
    if (container1.hasClass("yoket")) {
      container1.removeClass("yoket");
    } else if (!container1.hasClass("yoket")) {
      container1.addClass("yoket");
    }
  } else if (container1.has(e.target).length === 0) {
    container1.addClass("yoket");
  }
});

The reason it is not working for mobile(or any touch based device) is because the mouseup event is not fired, Please switch to click and/or touchend events to it. 它不适用于移动设备(或任何基于触摸的设备)的原因是未触发mouseup事件,请切换到它的click和/或touchend事件。 Ideally this should work. 理想情况下,这应该可行。

$(document).bind("mouseup touchend",function(e) {
  var label = $("#semt");
  var container1 = $("#semt1");

  if (label.has(e.target).length === 1) {
    if (container1.hasClass("yoket")) {
      container1.removeClass("yoket");
    } else if (!container1.hasClass("yoket")) {
      container1.addClass("yoket");
    }
  } else if (container1.has(e.target).length === 0) {
    container1.addClass("yoket");
  }
});

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM