On many websites I see links that have href="#"
. What does it mean? What is it used for?
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a way to play audio on a mobile browser w
- HTML form is not sending $_POST values
- implementing html5 drag and drop photos with knock
- React Native Inline style for multiple Text in sin
Putting the "#" symbol as the href for something means that it points not to a different URL, but rather to another id or name tag on the same page. For example:
However, if there is no id or name then it goes "no where."
Here's another similar question asked HTML Anchors with 'name' or 'id'?
About hyperlinks:
The main use of anchor tags -
<a></a>
- is as hyperlinks. That basically means that they take you somewhere. Hyperlinks require thehref
property, because it specifies a location.Hash:
A hash -
#
within a hyperlink specifies an html element id to which the window should be scrolled.href="#some-id"
would scroll to an element on the current page such as<div id="some-id">
.href="//site.com/#some-id"
would go tosite.com
and scroll to the id on that page.Scroll to Top:
href="#"
doesn't specify an id name, but does have a corresponding location - the top of the page. Clicking an anchor withhref="#"
will move the scroll position to the top.See this demo.
This is the expected behavior according to the w3 documentation.
Hyperlink placeholders:
An example where a hyperlink placeholder makes sense is within template previews. On single page demos for templates, I have often seen
<a href="#">
so that the anchor tag is a hyperlink, but doesn't go anywhere. Why not leave thehref
property blank? A blankhref
property is actually a hyperlink to the current page. In other words, it will cause a page refresh. As I discussed,href="#"
is also a hyperlink, and causes scrolling. Therefore, the best solution for hyperlink placeholders is actuallyhref="#!"
The idea here is that there hopefully isn't an element on the page withid="!"
(who does that!?) and the hyperlink therefore refers to nothing - so nothing happens.About anchor tags:
Another question that you may be wondering is, "Why not just leave the href property off?". A common response I've heard is that the
href
property is required, so it "should" be present on anchors. This is FALSE! Thehref
property is required only for an anchor to actually be a hyperlink! Read this from w3. So, why not just leave it off for placeholders? Browsers render default styles for elements and will change the default style of an anchor tag that doesn't have the href property. Instead, it will be considered like regular text. It even changes the browser's behavior regarding the element. The status bar (bottom of the screen) will not be displayed when hovering on an anchor without the href property. It is best to use a placeholder href value on an anchor to ensure it is treated as a hyperlink.See this demo demonstrating style and behavior differences.
Unordered lists are often created with the intent of using them as a menu, but an
li
list item is text. Because the listli
item is text, the mouse pointer will not be an arrow, but an "I cursor". Users are accustomed to seeing a pointing finger for a mouse pointer when something is clickable. Using an anchor taga
inside of theli
tag causes the mouse pointer to change to a pointing finger. The pointing finger is a lot better for using the list as a menu.If the list is being used for a menu, and doesn't need a link, then a URL doesn't need to be designated. But the problem is that if you leave out the
href
attribute, text in the<a>
tag is seen as text, and therefore the mouse pointer is back to an I-cursor. The I-cursor might make the user think that the menu item is not clickable. Therefore, you still need anhref
, but you don't need a link to anywhere.You could use lots of
div
orp
tags for a menu list, but the mouse pointer would be an I-cursor for them also.You could use lots of buttons stacked on top of each other for a menu list, but the list seems to be preferable. And that's probably why the
href="#"
that points to nowhere is used in anchor tags inside of list tags.You can set the pointer style in CSS, so that is another option. The
href="#"
to nowhere might just be the lazy way to set some styling.The problem with using href="#" for an empty link is that it will take you to the top of the page which may not be the desired action. To avoid this, for older browsers or non-HTML5 doctypes, use
As some of the other answers have pointed out, the
a
element requires anhref
attribute and the#
is used as a placeholder, but it is also a historical artifact.From Mozilla Developer Network:
Also, per the HTML5 spec:
Unfortunately, the most common use of
<a href="#">
is by lazy programmers who want clickable non-hyperlink javascript-coded elements that behave like anchors, but they can't be arsed to addcursor: pointer;
or:hover
styles to a class for their non-hyperlink elements, and are additionally too lazy to sethref
tojavascript:void(0);
.The problem with this is that one
<a href="#" onclick="some_function();">
or another inevitably ends up with a javascript error, and an anchor with an onclick javascript error always ends up following itshref
. Normally this ends up being an annoying jump to the top of the page, but in the case of sites using<base>
,<a href="#">
is handled as<a href="[base href]/#">
, resulting in an unexpected navigation. If any logable errors are being generated, you won't see them in the latter case unless you enable persistent logs.If an anchor element is used as a non-anchor it should have its
href
set tojavascript:void(0);
for the sake of graceful degradation.I just wasted two days debugging a random unexpected page redirect that should have simply refreshed the page, and finally tracked it down to a function raising the click event of an
<a href="#">
. Replacing the#
withjavascript:void(0);
fixed it.The first thing I'm doing Monday is purging the project of all instances of
<a href="#">
.