Events click jquery on iOS

0

Good morning, I've been struggling with the jQuery and iOS click events. I have tried several solutions, but they do not work. The first one that I tried was to add the cursor type to "pointer" to the elements in which I have the events. But it does not work.

.clickeable {
  cursor: pointer;
}

Then implement the detection of the iOS device and change the events from click to touchstart

var user_agent = navigator.userAgent.toLowerCase();
var event = user_agent.match(/(iphone|ipod|ipad)/)  ? "touchstart" : "click"; 
$("#menu-toggle").bind(event,function(e) {
    e.preventDefault();
    $('#menu-toggle').tooltip('hide');
    $("#wrapper").toggleClass("toggled");
    $("#menubtn")
 });

This only operated the menu button, but other events do not work. Like this:

$('.animadoespecial').bind(event,function (event) {
   event.preventDefault();
   $('html, body').animate({
      scrollTop: $( $.attr(this, 'href') ).offset().top -100
   }, 500);
});

I also tried adding the onClick attribute, but nothing works.

$('#menu-toggle').attr('onclick', '');

What could it be?

    
asked by NTHINGs 18.01.2017 в 23:18
source

1 answer

1
  

Please note: generally, on mobile devices, the touchstart event triggers immediately after the tap occurred, while the click event % triggers 300ms later. If you mix these it is possible to find yourself with the problem of ghost click .

This is wrong:

$('.animadoespecial').bind(event,function (event) { ... });

The first parameter must be the type of event that you want to associate with the element. The second, the callback to execute when it happens. The above should be:

// o touchstart
$('.animadoespecial').bind('click', function (event) { ... });

What does this mean?

$('#menu-toggle').attr('onclick', '');

It does not make any sense. You are only setting an empty string to the attribute onclick of the element.

Note: This method has been marked deprecated in version 3.x. It is recommended to use on instead:

// o touchstart
$('.animadoespecial').on('click', function (event) { ... });
    
answered by 18.01.2017 в 23:44