with jQuery? For th...">

How can I change the behavior of <a href= "https://stackoverflow.com/somewhere.html" id = "link" rel="nofollow noreferrer"> with jQuery?

For those with javascript enabled, click on them to do OnClick ()

For those with javascript disabled click on them, just go to where.html,

Assuming only $ ('# link') is available, how to do this?

+2


source to share


3 answers


Like this:

$('#link').click(function() { 
    //...
    return false;
});

      



Once written return false

, the link won't execute.

If Javascript is disabled, no click handler will be added and the link will behave normally.

+6


source


$ ("# link"). bind ("click", function (e) {alert ("clicked"); e.preventDefault (); return false;});



This will trigger "clicked" and then undo the default action. If Javascript is disabled the link will follow.

+2


source


Use a function click

. Add return false

to the end of your function to prevent the default action of clicking following the hyperlink:

$("#link").click(function() { onClick(); return false; });

      

+1


source







All Articles