How to make my 'click' function work with iOS

前端 未结 5 1722
面向向阳花
面向向阳花 2020-11-28 11:53

I have a set of Div\'s which act as buttons. These buttons have a simple jquery click() function which works in all browsers except iOS.

For example:



        
5条回答
  •  失恋的感觉
    2020-11-28 12:43

    Based on Marek's answer this is my take on it (it's a bit cleaned up):

    var ua = navigator.userAgent, 
    pickclick = (ua.match(/iPad/i) || ua.match(/iPhone/)) ? "touchstart" : "click";
    
    $('.clickable_element').on(pickclick, function(e) {
         // do something here
    });
    

    There's still a lot of work ahead for the industry when it comes to standards…

    EDIT:

    Didn't work out on Android phones. Took a more rigid approach:

    if (document.documentElement.clientWidth > 1025) { pickclick = 'click' }
    if (document.documentElement.clientWidth < 1025) { pickclick = 'touchstart' }
    
    $('.clickable_element').on(pickclick, function(e) {
         // do something here
    });
    

提交回复
热议问题