简体   繁体   English

如何在iOS中绑定点击事件?

[英]How to bind a click event in iOS?

I'm "just" trying to bind a click event to a <button> and make it work for Desktop (chrome, FF, ... ), android and iOS. 我只是在试图将click事件绑定到<button>并使之适用于桌面(chrome,FF,...),android和iOS。

I tried a lot of stuff such as this cursor: pointer which seemed to be the easiest and most effective but didn't work. 我尝试了很多东西,比如这个 cursor: pointer这似乎是最简单,最有效的,但没有奏效。

I also tried an onclick='' or onclick='void(0)' on the HTML element, a trick I've seen I don't even remember where ... 我还尝试了HTML元素上的onclick=''onclick='void(0)' ,这种技巧我什至不记得在哪里...

I tried with extern libraries such as FastClick to delete to 300ms wait after a click ... Doesn't work 我尝试使用外部库(例如FastClick)将其删除,直到单击后等待300毫秒。

And no more chance with Pointer Events Polyfill and binding with pointerup ... 指针事件Polyfill和使用pointerup绑定不再有机会...

I've run out of ideas ... 我的想法用光了...

My HTML : 我的HTML:

<div class="col-md-12">
    <button type="button" onclick='' id="begin" class="btn andro btn-danger btnlg">
        <img src="img/icon.png" height="50px"> GO
        <img src="img/icon.png" height="50px">
    </button>
</div>

My JS : 我的JS:

  $(document).on('touchstart', '#begin', function() {
      // do things
  });

I tried of course to bind touchstart (even touchend !) instead of click and still doesn't work on iphones ... 我当然尝试绑定touchstart (甚至touchend !)而不是click ,但在iPhone上仍然不起作用...

The problem is : I don't have an iphone to test my stuff so I have to trust some friends who have an iOS device ... Do I have to change my code ... Or change my friends ? 问题是:我没有iPhone来测试我的东西,所以我必须信任一些拥有iOS设备的朋友...我是否必须更改代码...还是要更改朋友? ^^ ^^

Soooo.... After a lot of searches and tries such as removing one by one libraries and commenting parts of the code... Soooo ....经过大量搜索和尝试,例如逐个删除库并注释部分代码...

I was able to find what was messing with the click ... It was because my bindings were encapsulated in a : 我能够找到导致点击混乱的原因……这是因为我的绑定被封装在一个:

$( document ).ready(function() {
    // My bindings here
});

and it seems like iOS don't like this ... 似乎iOS不喜欢这个...

Just bind your click out of the ready function and it works... Hope it can help someone ! 只需将您的单击绑定到ready函数之外,它便可以起作用...希望它能对某人有所帮助!

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

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