什么是href =“#”为什么它被使用? [英] What is href="#" and why is it used?

查看:132
本文介绍了什么是href =“#”为什么它被使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

关于超链接:



锚标签的主要用途 - < a>< / a> - 是 hyperlinks 即可。这基本上意味着他们带你到某个地方。超链接需要 href 属性,因为它指定了一个位置。

散列:



散列 - 内超链接指定窗口应该滚动到的html元素ID。



href =#some-id会滚动到当前页面上的元素,例如< div id =some-id>



href =// site.com/#some-id会去 site.com 并滚动到该页面上的ID。



滚动到顶部:



href =#不指定id名称,但确实有相应的位置 - 页面顶部。用 href =#点击一个锚点将会将滚动位置移动到顶部。

<一个href =http://jsfiddle.net/yrmnmd1q/>看这个演示。



这是根据 w3文档



超链接占位符:



超链接占位符合理的示例在模板预览中。在模板的单页演示中,我经常看到< a href =#> ,以便锚标记是超链接,但不会去任何地方。为什么不把 href 属性保留为空?空白 href 属性实际上是到当前页面的超链接。换句话说,它会导致页面刷新。正如我所讨论的, href =#也是一个超链接,并导致滚动。因此,超链接占位符的最佳解决方案实际上是 href =#!这里的想法是希望不是页面上带有 id =!(who does that !?),因此超链接指的是什么都没有 - 因此没有任何反应。
$ b 关于定位标记:

您可能想知道的另一个问题是,为什么不把href属性关闭?。我听到的一个普遍反应是, href 属性是必需的,所以它应该出现在锚点上。这是假的! href 属性仅适用于实际上是超链接的锚点!阅读 w3 。那么,为什么不把它留给占位符呢?浏览器渲染元素的默认样式,并将更改不具有href属性的锚点标签的默认样式。相反,它会被认为是普通文本。它甚至改变了浏览器对元素的行为。当悬停在没有href属性的锚上时,状态栏(屏幕底部)将不会显示。最好在锚点上使用占位符href值,以确保它被视为超链接。

查看此演示 ,展示风格和行为差异。


On many websites I see links that have href="#". What does it mean? What is it used for?

解决方案

About hyperlinks:

The main use of anchor tags - <a></a> - is as hyperlinks. That basically means that they take you somewhere. Hyperlinks require the href 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 to site.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 with href="#" 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 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.

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! 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.

See this demo demonstrating style and behavior differences.

这篇关于什么是href =“#”为什么它被使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆