Reputation: 2245
I am trying to build a webapp. when my site is run as an app on iPhone, every link I click closes the app and opens the page in Safari. Someone on this forum advised me to use the following.
$("a").click(function (event){
event.preventDefault();
window.location = $(this).attr('href');
});
this worked great and now all links open within the app. However, for some of my links, I now want to include an event.preventDefault();. Because of the code above, the preventDefault is overridden and the page still loads. For example:
$("a.myClass").live("click", function(event) {
event.preventDefault();
alert("testing");
});
<a href="http://google.com" class="myClass">Click me</a>
When both of the above scripts are on the page, the alert box clearly appears, but before you have time to read it, the page loads the link.
Is there a way around this?
Thanks
Upvotes: 1
Views: 151
Reputation: 15875
I am not sure.But try if it works:
$("a.myClass").live("click", function(event) {
event.preventDefault();
alert("testing");
return false;
});
Upvotes: 0
Reputation: 852
What about
$("a:not(.myClass)").click(function (event){
event.preventDefault();
window.location = $(this).attr('href');
});
Upvotes: 1