This question already has an answer here:
This function works perfectly on IE, Firefox and Chrome but when on the iPhone, it will only work when clicking on a <img>
. Clicking on the page (anywhere but on a img) wont fire the event.
$(document).ready(function () {
$(document).click(function (e) {
fire(e);
});
});
function fire(e) { alert('hi'); }
The HTML part is extremely basic and shouldnt be a problem.
Any ideas?
On mobile iOS the click event does not bubble to the document body and thus cannot be used with .live() events. If you have to use a non native click-able element like a div or section is to use cursor: pointer; in your css for the non-hover on the element in question. If that is ugly you could look into delegate().
Use jQTouch instead - its jQuery's mobile version
Change this:
To this
Maybe this solution don't fit on your work and like described on the replies this is not the best solution to apply. Please, check another fixes from another users.
CSS
Cursor:Pointer;
is a great solution. FastClick https://github.com/ftlabs/fastclick is another solution which doesn't require you to change css if you didn't wantCursor:Pointer;
on an element for some reason. I use fastclick now anyway to eliminate the 300ms delay on iOS devices.Include this to your project. Check the "Readme" on github. https://github.com/tomasz-swirski/iOS9-Safari-Click-Fix
Short answer:
Longer answer:
It's important to realize that if you're just using
<a>
tags everything will work as expected. You can click or drag by mistake on a regular<a>
link on an iPhone and everything behaves as the user would expect.I imagine that you have arbitrary HTML that is not clickable - such as a panel containing text and images that cannot be wrapped with
<a>
. I found out about this problem when I had such a panel that I wanted to be entirely clickable.To detect a click anywhere within this div I am using jQuery with a
data-href
html attribute which is shown above (this attribute is invented by myself and is not a standard jQuery or HTML data attribute.)This will work on your desktop browser but not iPad no matter how much you click.
You may be tempted to change your event handler from
click
toclick touchstart
- and this indeed does trigger the event handler. However if the user wants to drag the page up (to scroll) they'll trigger it too - which is a terrible user experience. [you may have noticed this behavior by sneaky banner ads]The answer is incredibly simple: Just set the css
cursor: pointer
.This had the added benefit for desktop users to indicate the area is clickable with a hand icon.
Thanks to https://stackoverflow.com/a/4910962/16940