I use jQuery, I need to make some anchor tags perform no action.
I usually write it like this:
link
H
There are a few less than perfect solutions:
1. Link to a fake anchor
<a href="#">
Problem: clicking the link jumps back to the top of the page
2. Using a tag other than 'a'
Use a span tag and use the jquery to handle the click
Problem: breaks keyboard navigation, have to manually change the hover cursor
3. Link to a javascript void function
<a href="javascript:void(0);">
<a href="javascript:;">
Problem: breaks when linking images in IE
Solution
Since these all have their problems, the solution I've settled on is to link to a fake anchor, and then return false from the onClick method:
<a href="#" onClick="return false;">
Not the most concise of solutions, but it solves all the problems with the above methods.
If you don't want to have it point to anything, you probably shouldn't be using the <a>
(anchor) tag.
If you want something to look like a link but not act like a link, it's best to use the appropriate element (such as <span>
) and then style it using CSS:
<span class="fake-link" id="fake-link-1">Am I a link?</span>
.fake-link {
color: blue;
text-decoration: underline;
cursor: pointer;
}
Also, given that you tagged this question "jQuery", I am assuming that you want to attach a click event hander. If so, just do the same thing as above and then use something like the following JavaScript:
$('#fake-link-1').click(function() {
/* put your code here */
});
<a href="#" onclick="SomeFunction()" class="SomeClass">sth.</a>
this was my anchor tag. so return false on onClick="" event is not usefull here. I just removed href="#" property and it worked for me just like below
<a onclick="SomeFunction()" class="SomeClass">sth.</a>
and i needed to add this css.
.SomeClass
{
cursor: pointer;
}
The only thing that worked for me was a combination of the above:
First the li
in the ul
<li><a onclick="LoadTab2_1()" href="JavaScript:void(0)">All Assigned</a></li>
Then in the LoadTab2_1 I manually switched the tab divs.
$("#tabs-2-1").hide();
$("#tabs-2-2").show();
This is because the disconnection of the also disconnects the action in the tabs.
You also need to manually do the tab styling when the primary tab changes things.
$("#secTab1").addClass("ui-tabs-active").addClass("ui-state-active").addClass("ui-state-hover").addClass("ui-state-focus");
$("#secTab1 a").css("color", "#ffffff");
The correct way to handle this is to "break" the link with jQuery when you handle the link
HTML
<a href="#" id="theLink">My Link</a>
JS
$('#theLink').click(function(ev){
// do whatever you want here
ev.preventDefault();
ev.stopPropagation();
});
Those final two calls stop the browser interpreting the click.
I think you can try
<a href="JavaScript:void(0)">link</a>
The only catch I see over here is high level browser security may prompt on executing javascript.
Though this is one of the easier way than
<a href="#" onclick="return false;">Link</a>
this should be used sparingly
Read this article for some pointers https://web.archive.org/web/20090301044015/http://blog.reindel.com/2006/08/11/a-hrefjavascriptvoid0-avoid-the-void