在许多网站上,我看到有href="#"的链接。这是什么意思?它的用途是什么?


当前回答

据我所知,它通常是一个占位符的链接,有一些JavaScript附加到他们。链接的要点是通过执行JavaScript代码来实现的;支持JS的浏览器会忽略真正的链接目标。如果浏览器不支持JS,散列标记实际上会将链接变成一个no - op。参见unobtrusive JavaScript。

其他回答

不幸的是,<a href="#">最常见的使用是由懒惰的程序员使用的,他们想要可点击的非超链接javascript编码的元素,这些元素的行为像锚一样,但它们不能被用来添加游标:指针;或者:悬停样式到类的非超链接元素,并且懒得将href设置为javascript: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 its href. 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.

如果一个锚元素被用作非锚元素,它的href应该设置为javascript:void(0);为了优雅的堕落。

我只是浪费了两天时间调试一个随机的、意外的页面重定向,它本应该只是简单地刷新页面,最后追踪到一个引发<a href="#">点击事件的函数。将#替换为javascript:void(0);固定它。

我星期一要做的第一件事是清除<a href="#">的所有实例的项目。

它实际上是一个不指向任何地方的链接(它只是在URL上添加了“#”)。它被用于许多不同的原因。例如,如果您正在使用某种JavaScript/jQuery,并且不希望实际的HTML链接到任何地方。

它还用于页面锚,用于重定向到页面的不同部分。

将“#”符号作为某个东西的href意味着它不是指向不同的URL,而是指向同一页面上的另一个id或名称标记。例如:

<a href="#bottomOfPage">Click to go to the bottom of the page</a>
blah blah
blah blah
...
<a id="bottomOfPage"></a>

然而,如果没有id或名称,那么它就会变成“no where”。

这里有另一个类似的问题问HTML锚与'名称'或'id'?

对于空链接使用href="#"的问题是,它会把你带到页面的顶部,这可能不是你想要的操作。 为了避免这种情况,对于旧的浏览器或非html5文档类型,使用

<a href="javascript:void(0)">Goes Nowhere</a>

href属性定义了链接资源的URL。如果锚标签没有href标签,那么它就不会成为超链接。href属性有以下值:

1. Absolute path: move to another site like href="http://www.google.com"
2. Relative path: move to another page within the site like herf ="defaultpage.aspx"
3. Move to an element with a specified id within the page like href="#bottom"
4. href="javascript:void(0)", it does not move anywhere.
5. href="#" , it does not move anywhere but scroll on the top of the current page.
6. href= "" , it will load the current page but some browsers causes forbidden errors.

Note: When we do not need to specified any url inside a anchor tag then use 
<a href="javascript:void(0)">Test1</a>