Html 当链接中有井号“#”时是什么意思

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2800187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:00:14  来源:igfitidea点击:

What is it when a link has a pound "#" sign in it

html

提问by Luke101

I have inspected some sites and they have a pound(#) sign in the url. What does it do?

我检查了一些网站,他们在 url 中有一个井号 (#) 符号。它有什么作用?

 <a href="#" >Link name</a>

回答by Dean Harding

It's a "fragment" or "named anchor". You can you use to link to partof a document. Typically when you link to a page, the browser opens it up at the top of the page. But you link to a section half-way down, you can use the fragment to link to that heading (or whatever).

它是一个“片段”或“命名锚”。您可以使用链接到文档的一部分。通常,当您链接到一个页面时,浏览器会在页面顶部打开它。但是您链接到一半的部分,您可以使用片段链接到该标题(或其他任何内容)。

If there is no <a name="whatever"/>tag within the page, then the browser will just link to the top of the page. If the fragment is empty, then it will also just link to the top of the page.

如果<a name="whatever"/>页面中没有标签,那么浏览器只会链接到页面顶部。如果片段是空的,那么它也只会链接到页面的顶部。

For a fragment only<a href="#">Link name</a>, then that's just a link to the top of the currentpage.

对于片段only<a href="#">Link name</a>,那么这只是指向当前页面顶部的链接。

You often see that kind of link used in conjuction with javascript. Standards compliant HTML requires a hrefattribute, but if you're planning to handle the request with javascript then "#" serves as a reasonable place holder.

您经常会看到与 javascript 结合使用的那种链接。符合标准的 HTML 需要一个href属性,但如果您打算使用 javascript 处理请求,那么“#”将作为一个合理的占位符。

回答by alex

... just to add a few extra useful tips.

...只是添加一些额外的有用提示。

You can access and change it with document.location.hashin JavaScript.

您可以document.location.hash在 JavaScript 中访问和更改它。

It can point to a named anchor (e.g. <a name="top"></a>) orto an element with a corresponding id (e.g. <div id="top"></div>).

它可以指向一个命名的锚点(例如<a name="top"></a>具有相应 id 的元素(例如<div id="top"></div>)。

Seeing one on its own (e.g. <a href="#" onclick="pop()">popup</a>) generally means a link is being used to run JavaScript exclusively. This is bad practice.

单独看到一个(例如<a href="#" onclick="pop()">popup</a>)通常意味着一个链接被用来专门运行 JavaScript。这是不好的做法。

Any aelement should have a hrefthat points to a valid resource. If one does not exist, consider using another element, such as button.

任何a元素都应该有一个href指向有效资源的 。如果不存在,请考虑使用另一个元素,例如button

回答by Nathan Osman

#indicates a link to an anchor.

#表示指向锚点的链接。

I thougt I'd also mention something else:

我想我还会提到别的东西:

Using '#' as the href for a link that activates JavaScript is bad because it scrolls the page to the top - which is probably not what you want. Instead, use javascript:void(0).

使用“#”作为激活 JavaScript 的链接的 href 是不好的,因为它会将页面滚动到顶部 - 这可能不是您想要的。相反,使用javascript:void(0).

回答by ElectroBit

The pound sign (#) indicates to locate an anchor on the page. For example, if you include this somewhere on the page:

井号 ( #) 表示在页面上定位锚点。例如,如果您在页面的某处包含此内容:

<a name="foo"></a>

or, more recently:

或者,最近:

<div id="foo">*part of page*</div>

and then you click on a link on the page that has the href #foo, it will navigate to the anchor with the name or divwith the id foo.

然后您单击页面上具有 href 的链接#foo,它将导航到具有名称或divid的锚点foo

However, if you just have the href #, it will lead to the top of the page.

但是,如果您只有 href #,它将导致页面顶部。

回答by ElectroBit

This links back to the page itself. It's often used with links which actually run some JavaScript.

这链接回页面本身。它通常与实际运行一些 JavaScript 的链接一起使用。