在许多网站上,我看到有href="#"的链接。这是什么意思?它的用途是什么?
当前回答
不幸的是,<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="#">的所有实例的项目。
其他回答
据我所知,它通常是一个占位符的链接,有一些JavaScript附加到他们。链接的要点是通过执行JavaScript代码来实现的;支持JS的浏览器会忽略真正的链接目标。如果浏览器不支持JS,散列标记实际上会将链接变成一个no - op。参见unobtrusive JavaScript。
超链接:
锚标记(<a></a>)的主要用途是作为超链接。这基本上意味着他们会带你去某个地方。超链接需要href属性,因为它指定了一个位置。
散列:
A hash - `#` within a hyperlink specifies an HTML element id to which the window should be scrolled.Href ="#some-id"将滚动到当前页面上的一个元素,例如<div id="some-id">。
href = " / /网站。示例:/#some-id”将访问站点。示例并滚动到该页上的id。
滚动到顶部:
Href ="#"没有指定id名称,但是有一个对应的位置——页面顶部。点击href="#"的锚将滚动位置移到顶部。
请看这个演示。
根据w3文档,这是预期的行为。
超链接占位符:
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 the href property blank? A blank href 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 actually href="#!" The idea here is that there hopefully isn't an element on the page with id="!" (who does that!?) and the hyperlink therefore refers to nothing - so nothing happens.
关于锚标签:
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! The href 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.
请参阅演示样式和行为差异的演示。
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>
不幸的是,<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="#">的所有实例的项目。
将“#”符号作为某个东西的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'?