Html 什么是#top 以及如何在我的网站上使用它

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

What is #top and how can I use it on my site

htmlhyperlink

提问by 123

I always see websites that has a link that says #top or #bottom that takes you to the top or bottom of the page. Can someone please tell me how I can use it on my website. I already tried saying <a href="#top">or <a href="#bottom">but it did not work.

我总是看到网站上有一个链接,上面写着 #top 或 #bottom,可以将您带到页面的顶部或底部。有人可以告诉我如何在我的网站上使用它。我已经试过说<a href="#top">or<a href="#bottom">但它没有用。

回答by Jukka K. Korpela

This depends on what exactly you would like to be treated as top and bottom. To link to the very start of the page, you can use the URL #, as in <a href="#">Start of page</a>. To link to some specific element near the start, assign an idattribute to it, e.g. <h1 id="top">Main heading</h1>, and use that attribute value in a link, e.g. <a href="#top">Start of page</a>.

这取决于您希望被视为顶部和底部的确切内容。要链接到页面的最开始,您可以使用 URL #,如<a href="#">Start of page</a>。要链接到开头附近的某个特定元素,请为其分配一个id属性,例如<h1 id="top">Main heading</h1>,并在链接中使用该属性值,例如<a href="#top">Start of page</a>

The bottom is a bit more tricky, since there is no predefined URL for it, and although you can use the idtechnique, the URL will refer to the startof the element. You could deal with this using an empty element at the very end of document body:

底部有点棘手,因为它没有预定义的 URL,尽管您可以使用该id技术,但 URL 将引用元素的开头。您可以在文档正文的最后使用一个空元素来处理这个问题:

Last piece of real content.
<div id="bottom"></div>
</body>
</html>

Then you would use e.g. <a href="#bottom">End of page</a>.

然后你会使用 eg <a href="#bottom">End of page</a>

However, normally links to start of page are worse than useless, and links to end of page are no better. Every browser provides a simple way of getting to the start or to the end of any page.

但是,通常指向页面开头的链接比无用更糟糕,指向页面结尾的链接也好不到哪里去。每个浏览器都提供了一种进入任何页面开始或结束的简单方法。

回答by Christopher Marshall

Hashing with an idwill take you to the equivalent element with the idon the page.

使用 散列id将带您到id页面上的等效元素。

So if you have a div like so:

所以如果你有一个像这样的 div:

<div id="top"></div>

and an anchor as such:

和一个锚点:

<a href="#top">Go to top div</a>

Clicking the anchor will take you to that divs place in the DOM.

单击锚点将带您到 DOM 中的那个 div 位置。

回答by user2805476

Quite simple if a user is at the bottom of the page show them

很简单,如果用户在页面底部显示他们

<a href="#top">Go to the top</a>

Or if they are at the top of the page show them

或者如果他们在页面顶部显示他们

<a href="#bottom">Go to bottom</a>