开发者

How do I use jQuery for click event in iPhone web application

开发者 https://www.devze.com 2023-01-03 08:42 出处:网络
When I use jQuery for a simple click event it only works for links. Is there a way to make it work for spans etc:

When I use jQuery for a simple click event it only works for links. Is there a way to make it work for spans etc:

$("span.clicked").live("click", function(e){alert("span clicked!")});

$("a.clicked").live("click", function(e){alert("link clicked!")});

The SPAN works in Safari but not Mobile Safari (on iPhone or iPad) whereas the A tag works in开发者_StackOverflow both.


I struggled with this as well. After lots of toying around and trying to figure out the problem, I came across a simple solution.

If you set the element's cursor to pointer, it magically works again with Jquery's live and the click event. This can just be set globally in the CSS.


You need to listen for the "touchstart" and "touchend" events. Add the listeners with jQuery...

$('span').bind( "touchstart", function(e){alert('Span Clicked!')} );

You may wish to listen for a touchstart and touchend so that you can verify that the element targeted when the finger touched is the same as the element targeted when the finger was removed.

I'm sure there is probably a better way to do it but that should work :)

Edit: There is a better way! See https://stackoverflow.com/a/4910962/16940


You actually don't need to use the touchstart or touchend event, so long as the 'span' tag (or anything other than an 'a' tag) has a css property of:

cursor:pointer

the click will register


You can also coax the browser to generate click events by adding an empty onclick attribute. For a belt-and-braces approach in case either approach stops working in any given iOS update, you could use something like this:

$("span.clicked").live("click", function(e){alert("span clicked!")})
                 .attr('onclick','')
                 .css('cursor','pointer');

(assuming you don't have any actual onclick attributes you don't mind obliterating)


You can add an empty onclick attribute, like so:

<span onclick=''>Touch or Click Me</span>
jQuery('span').live('click', function() { alert('foo'); });


I tried everything and none of the tricks worked. It turned out I couldn't get click events because I had a video element under my img. video elements apparently eat click events.


When targeting iOS you have to take the following into consideration: doing event delegation in jQuery like $(document).on('click', '.target-element', function (event) {...}); will not work. You have to add either onclick="" to the target HTML element or cursor: pointer to its styles.

Taken and adapted from http://gravitydept.com/blog/js-click-event-bubbling-on-ios:

It turns out that Safari on the iPhone does not support event delegation for click events, unless the click takes place on a link or input. Fortunately there are workarounds available.

That's the reason while the <a> tag works while <span> doesn't.


My approach to solve this misunderstanding on document.click.

  1. Add into html after tag body next tag

    <body> <div id="overlaySection" onclick="void(0)"></div> ... </body>

  2. Some style for that tag

    `#overlaySection {
      position: fixed;
      top: 0;
      left: 0;
      z-index: 1;
      width: 100%;
      height: 100%;
      background: rgba(255, 255, 255, 0);
      cursor: pointer;
      visibility: hidden;
      opacity: 0;
      content: "";
    }
    #overlaySection.active {
     opacity: 1;
     visibility: visible;
    }`
    
  3. Some JQuery staff

    // hide overlay on document click $('#overlaySection').click(function(){ $(this).removeClass('active'); });

and the main thing to active this overlay

$('.dropdown > span').click(function() {
    ...
    $('#overlaySection').addClass('active');
    ...
});

Hope this approach will be useful to someone. Happy coding!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号