Html 如何链接到页面的一部分?(哈希?)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2835140/
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:05:56  来源:igfitidea点击:

How do I link to part of a page? (hash?)

htmlxhtmlanchor

提问by Haroldo

How do you link (with <a>) so that the browser goes to certain subheading on the target page as opposed to the top?

您如何链接(使用<a>)以便浏览器转到目标页面上的某些副标题而不是顶部?

回答by Daniel DiPaolo

If there is an <a name="foo">tag or any tag with an id(e.g., <div id="foo">), then you can simply append #footo the URL. Otherwise, you can't arbitrarily link to portions of a page.

如果有一个<a name="foo">标签或任何带有id(例如,<div id="foo">)的标签,那么您可以简单地附加#foo到 URL。否则,您不能随意链接到页面的某些部分。

Here's a complete example: <a href="http://example.com/page.html#foo">Jump to #foo on page.html</a>

这是一个完整的例子: <a href="http://example.com/page.html#foo">Jump to #foo on page.html</a>

Linking content on the same page example: <a href="#foo">Jump to #foo on same page</a>

链接同一页面上的内容示例: <a href="#foo">Jump to #foo on same page</a>

回答by Michael Aaron Safyan

You use an anchor and a hash. For example:

您使用锚点和哈希。例如:

Target of the Link:

链接目标:

 <a name="name_of_target">Content</a>

Link to the Target:

目标链接:

 <a href="#name_of_target">Link Text</a>

Or, if linking from a different page:

或者,如果从其他页面链接:

 <a href="http://path/to/page/#name_of_target">Link Text</a>

回答by Felix Kling

Just append a hash with an ID of an element to the URL. E.g.

只需将带有元素 ID 的哈希附加到 URL。例如

<div id="about"></div>

and

http://mysite.com/#about

So the link would look like:

所以链接看起来像:

<a href="http://mysite.com/#about">About</a>

or just

要不就

<a href="#about">About</a>

回答by Sarfraz

Here is how:

方法如下:

<a href="#go_middle">Go Middle</a>

<div id="go_middle">Hello There</div>

回答by Daniel Vassallo

You have two options:

您有两个选择:

You can either put an anchor in your document as follows:

您可以按如下方式在文档中放置一个锚点:

<a name="ref"></a>

Or else you give an id to a any HTML element:

或者你给任何 HTML 元素一个 id:

<h1 id="ref">Heading</h1>

Then simply append the hash #refto the URL of your link to jump to the desired reference. Example:

然后只需将哈希附加#ref到链接的 URL 即可跳转到所需的引用。例子:

<a href="document.html#ref">Jump to ref in document.html</a>

回答by Abderrahmane TAHRI JOUTI

On 12 March 2020, a draft has been added by WICG for Text Fragments, and now you can link to text on a page as if you were searching for it by adding the following to the hash

2020 年 3 月 12 日,WICG 为文本片段添加了一个草稿,现在您可以通过将以下内容添加到哈希中来链接到页面上的文本,就像您在搜索它一样

#:~:text=<Text To Link to>

#:~:text=<Text To Link to>

Working example on Chrome Version 81.0.4044.138:

工作示例Chrome Version 81.0.4044.138

Click on this linkShould reload the page and highlight the link's text

单击此链接应重新加载页面并突出显示链接的文本