How to disable the default behavior of an Anchor in jQuery Mobile (iOS)

后端 未结 5 1328
悲哀的现实
悲哀的现实 2020-12-05 08:26

I am building an app for iPhone and iPad using PhoneGap and jQM


                      
相关标签:
5条回答
  • 2020-12-05 09:14

    When you do $('a').click( .. ) you're only processing the 'click' event. Here it's a different event and actually a system event for iOS that you cannot handle in javascript.

    So you'll have to disable this feature completely from your webapp if you don't want it.

    Try the following:

    <script>
    document.documentElement.style.webkitTouchCallout = 'none';
    </script>
    

    Or in your CSS:

    a[data-role=button] {
        -webkit-user-select: none!important;
    }
    
    0 讨论(0)
  • 2020-12-05 09:21

    ok got it to work,

    I had to combine both code fixes, CSS and JavaScript

    so in my CSS I did this:

    body { -webkit-touch-callout: none !important; }
    a { -webkit-user-select: none !important; }
    

    in my JavaScript did this:

    function onBodyLoad() {
      $("a").bind('taphold', function(event) {
      event.preventDefault();
     });
    }
    

    and now all the links are disabled, but if I attach an event to any of them it will work with no problem

    THanks all

    0 讨论(0)
  • 2020-12-05 09:21
    <style type="text/css">
     *:not(input):not(textarea) {
     -webkit-user-select: none; /* disable selection/Copy of UIWebView */
     -webkit-touch-callout: none; /* disable the IOS popup when long-press on a link */
    }       
    </style>
    
    **If you want Disable only anchor button tag use this.**
     a {-webkit-user-select: none; /* disable selection/Copy of UIWebView */
      -webkit-touch-callout: none; /* disable the IOS popup when long-press on a link */
     }
    
    0 讨论(0)
  • 2020-12-05 09:24

    Have a look at the events fired by JQM here http://jquerymobile.com/demos/1.1.0/docs/api/events.html. You want to handle the "taphold" event.

    EDIT Soon after posting this I ended up seeing the same problem in my app! I found that adding this style, similar to what @chrisben suggested fixes it:

    body {
        -webkit-touch-callout: none !important;
    }
    

    I don't have any form elements on my app so I don't know about those but links and buttons all work perfectly fine with this style added.

    0 讨论(0)
  • 2020-12-05 09:24

    Simplest way to get this to work on iPhone is to disable the webkit touch styles:

    document.getElementById('your element id').style.webkitTouchCallout = 'none';
    
    0 讨论(0)
提交回复
热议问题