Html 获取链接以转到另一页上的特定部分

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

Getting a link to go to a specific section on another page

htmlhyperlinkanchor

提问by ledgeJumper

I have a link on one page that needs to go to a different page, but load to a specific section on that other page.

我在一个页面上有一个链接,需要转到另一个页面,但加载到另一个页面上的特定部分。

I have done this before with bootstrap but they take all the 'coding' out of it, so I need to know how to do from scratch. Here is the markup I have based on this link (not the best resource, I know): http://www.w3schools.com/html/html_links.asp

我以前用 bootstrap 做过这件事,但他们把所有的“编码”都去掉了,所以我需要知道如何从头开始。这是我基于此链接的标记(我知道不是最好的资源):http: //www.w3schools.com/html/html_links.asp

**Page One**
<a href="/academics/page.html#timeline> Click here </a>
**Page I am linking to**
<div id="timeline" name="timeline"> ... </div>

Can I do this with just HTML, or do I need some JavaScript? If I need to do it via JS, it needs to be on the target page, right?

我可以只用 HTML 来做到这一点,还是需要一些 JavaScript?如果我需要通过 JS 来做,它需要在目标页面上,对吗?

回答by Mike Christensen

I believe the example you've posted is using HTML5, which allows you tojump to anyDOM element with the matching IDattribute. To support older browsers, you'll need to change:

我相信您发布的示例使用的是 HTML5,它允许您跳转到具有匹配属性的任何DOM 元素ID。要支持旧浏览器,您需要更改:

<div id="timeline" name="timeline" ...>

To the old format:

旧格式:

<a name="timeline" />

You'll then be able to navigate to /academics/page.html#timelineand jump right to that section.

然后,您将能够导航/academics/page.html#timeline到该部分并直接跳转到该部分。

Also, check out this similar question.

另外,看看这个类似的问题

回答by mayur nandu

Hiii

you can simply use

你可以简单地使用

 <a href="directry/filename.html#section5" >click me</a>

to link to a section/id of another page by

通过以下方式链接到另一个页面的部分/ID

回答by Juliette

I tried the above answer - using page.html#ID_nameit gave me a 404 page doesn't existerror.

我尝试了上面的答案 - 使用page.html#ID_name它给了我一个404 页面不存在错误。

Then instead of using .html, I simply put a slash /before the #and that worked fine. So my example on the sending page between the link tags looks like:

然后.html,我没有使用,而是简单地/在 之前放了一个斜杠#,效果很好。所以我在链接标签之间的发送页面上的示例如下所示:

<a href= "http://my website.com/target-page/#El_Chorro">El Chorro</a>

Just use /instead of .html.

只需使用/代替.html.

回答by Olagunju Emmanuel Art

To link from a page to another section just use

要将一个页面链接到另一个部分,只需使用

<a href="index.php#firstdiv">my first div</a>