views:

1179

answers:

6

Hello How to make the "< a href" tag refer to nothing?

because I use jQuery

I need to make some anchor taks refer to nothing

I usually write it like this

<a href="#">link</a>

however this refer to the top of the page!

+4  A: 

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'm 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 */
});
Tyson
This has a problem: it doesn't allow tab-navigation.
Note that (as Iraimbilanja points out) the span approach here disables the possibility to invoke the function using the keyboard, which I would consider a usability problem.
Fredrik Mörk
Also an accessibility problem. You can resolve it by using a button element (generated from JavaScript so non-JS users don't get a broken control).
David Dorward
If it's a button that doesn't have a plain HTML fallback (links to a page or an anchor), then making it tab-navigable doesn't really serve much purpose for screen readers (of course, I could be wrong because I'm not too up-to-date on screen readers' JS capabilities).
Tyson
Here's a quick fix for that: tabindex="0" on a non-link element will enable keyboard navigation in most modern browsers.
Mathias Bynens
@Tyson Screen readers read what is on the screen, if the browser supports JS, then so does the screen reader. Of course, the JS might display information in a way that doesn't draw the attention of the reader, or do something that depends on sight to use - but that is a different issue. Generating the button from JS avoids presenting a useless control to users without JS.
David Dorward
Another 'problem' is that you don't the CSS :hover pseudo class won't work on elements that aren't links in IE6. But since you're adding these elements through JavaScript anyway (at least, you should), you could just as well write a script that adds a .hover class to the element when hovered.
Mathias Bynens
+9  A: 

What do you mean by nothing?

<a href='about:blank'>blank page</a>

or

<a href='whatever' onclick='return false;'>won't navigate</a>
KristoferA - Huagati.com
Agreed - just return false from your jQuery method and voila, it won't go anywhere
joshcomley
+9  A: 

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.

Oli
While that does sound like it's "proper" because it stops event bubbling, "return false" is a lot less verbose and does the same thing. The only time you would want to do that is if you already have event handlers registered to clicks on links via jQuery.
aleemb
+5  A: 

To make it do nothing at all, use this:

<a href="javascript:void(0)"> ... </a>
Philippe Leybaert
A: 

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 http://blog.reindel.com/2006/08/11/a-hrefjavascriptvoid0-avoid-the-void/

Rutesh Makhijani
why the downvote?
the_drow
+5  A: 

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.

zaius