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


当前回答

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>

其他回答

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

正如其他一些回答所指出的,a元素需要href属性,#用作占位符,但它也是一个历史工件。

来自Mozilla开发者网络:

href 这是锚定义的惟一必需属性 超文本源链接,但在HTML5中不再需要。省略 此属性创建一个占位符链接。href属性 链接目标,URL或URL片段。一个URL Fragment是一个名称,前面有一个散列标记(#),它指定了一个 当前文档中的内部目标位置(ID)。

此外,根据HTML5规范:

如果a元素没有href属性,则该元素表示a 占位符,用于链接可能被放置的位置 都是相关的,只包含元素的内容。

对于空链接使用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>

将“#”符号作为某个东西的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'?