How to enable or disable an anchor using jQuery?
相关问题
- Views base64 encoded blob in HTML with PHP
- How to fix IE ClearType + jQuery opacity problem i
- jQuery add and remove delay
- Is there a way to play audio on a mobile browser w
- HTML form is not sending $_POST values
If this method is called, the default action of the event will not be triggered.
If you don't need it to behave as an anchor tag then I would prefer to replace it at all. For example if your anchor tag is like
then using jquery you can do this when you need to display text instead of a link.
So this way, we can simply replace anchor tag with a div tag. This is much easier (just 2 lines) and semantically correct as well (because we don't need a link now therefore should not have a link)
To prevent an anchor from following the specified
href
, I would suggest usingpreventDefault()
:See:
http://docs.jquery.com/Events/jQuery.Event#event.preventDefault.28.29
Also see this previous question on SO:
jQuery disable a link
Disable or Enable any element with the disabled property.
I think a nicer solution is to set disabled data attribute on and anchor an check for it on click. This way we can disable temporarily an anchor until e.g. the javascript is finished with ajax call or some calculations. If we do not disable it, then we can quickly click it a few times, which is undesirable...
I made a jsfiddle example: http://jsfiddle.net/wgZ59/76/
I found an answer that I like much better here
Looks like this:
Enabling would involve setting the href attribute
This gives you the appearance that the anchor element becomes normal text, and vice versa.