tags:

views:

278

answers:

3

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

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

Suppose only $('#link') is accessible,how to do that?

A: 

Use the click function. Add return false to the end of your function to prevent the default action of the click, which is following the hyperlink:

$("#link").click(function() { onClick(); return false; });
Rudd Zwolinski
OnClick is a function, no point in wrapping it in a thunk.
Aaron Maenpaa
Now there is - `return false`.
SLaks
+1  A: 

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

This will alert "clicked" and then cancel the default action. If Javascript is disabled, the link is followed.

Marius
What's the difference between $.bind and $.click ?
Shore
$.bind also takes the name of the event to bind to: http://docs.jquery.com/Events/bind#typedatafn This answer is wrong and will not work because it doesn't pass the event name.
SLaks
Thanks SLaks, forgot to add the "click" argument. Fixed
Marius
+4  A: 

Like this:

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

By writing return false, the link will not be followed.

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

SLaks