I use jQuery, I need to make some anchor tags perform no action.
I usually write it like this:
link
H
You can have an HTML anchor (a
tag) without an href
attribute. Leave off the href
attribute & it won't link to anything:
<a>link</a>
I know this is tagged as a jQuery question, but you can answer this with AngularJS, also.
in your element, add the ng-click directive and use the $event variable which is the click event... prevent its default behavior:
<a href="#" ng-click="$event.preventDefault()">
You can even pass the $event variable into a function:
<a href="#" ng-click="doSomething($event)">
in that function, you do whatever you want with the click event.
I know this is an old question, but I thought I'd add my two cents anyway:
It depends on what the link is going to do, but usually, I would be pointing the link at a url that could possibly be displaying/doing the same thing, for example, if you're making a little about box pop up:
<a id="about" href="/about">About</a>
Then with jQuery
$('#about').click(function(e) {
$('#aboutbox').show();
e.preventDefault();
});
This way, very old browsers (or browsers with JavaScript disabled) can still navigate to a separate about page, but more importantly, Google will also pick this up and crawl the contents of the about page.
Here are the three ways for <a>
tag's href
tag property refer to nothing:
<a href="JavaScript:void(0)"> link </a>
<a href="javascript:;">link</a >
<a href="#" onclick="return false;"> Link </a>
Make sure all your links that you want to stop have href="#!"
(or anything you want, really), and then use this:
jq('body').on('click.stop_link', 'a[href="#!"]',
function(event) {
event.preventDefault();
});
To make it do nothing at all, use this:
<a href="javascript:void(0)"> ... </a>