This is a dynamic JavaScript application I am working on. I have many <a>
anchor elements that have a class. When that class is clicked, something should happen. This works fine in Firefox, Chrome, IE, but in some cases the click event is not triggered on mobile Safari (iPad and iPhone).
These elements all have exactly the same CSS, it’s just their position that differs (they are in different containers).
I tried various solutions that I found here but with no luck. For instance:
- setting the cursor to pointer
- the code in this answer
Do you have any other idea that might help me find a solution to this? Why does the click event triggers only in some cases?
Advertisement
Answer
The click event resulting from a tap on iOS will bubble as expected under just certain conditions — one of which you mentioned, the cursor style. Here is another:
The target element, or any of its ancestors up to but not including the
<body>
, has an explicit event handler set for any of the mouse events. This event handler may be an empty function.
http://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html
This is much better fix vector, and can be done without a browser check. You do have to add an extra div due to the “not including the <body>
” part:
<body> <div onclick="void(0);"> <!-- ... all your normal body content ... --> </div> </body>
Now every click event originating inside that div will bubble as expected in iOS (meaning you can catch them with $(document).on('click', '.class', etc...)
).