如何在动态 url 中使用 HTML # 锚点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12096614/
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
How to use HTML # anchor in a dynamic url
提问by bodesam
I want to link to a section of a dynamic page using the # anchor, something like this:
我想使用 # 锚链接到动态页面的一部分,如下所示:
<a href=page.php?id=3#section-name>LINK</a>
It didn't work. What is the right way to do it?
它没有用。正确的做法是什么?
回答by James Hill
You've only presented half of your code so I can only give a sample of the proper way to do it:
你只提供了一半的代码,所以我只能给出一个正确方法的示例:
<body>
<a name="top"> </a>
<a href="#top">
Go To Top Of Page
</a>
</body>
回答by Vin Burgh
When using anchor tags, you can target an element by its ID. Browsers will look for the ID before it looks for the name
attribute when the link refers to such.
使用锚标记时,您可以按元素 ID 定位元素。name
当链接指向这样的属性时,浏览器会在查找属性之前查找 ID 。
<a href="#section-name>LINK</a>
will go directly to <div id="section-name">
if it exists.
<a href="#section-name>LINK</a>
<div id="section-name">
如果存在,将直接转到。
回答by jasonleonhard
A typical anchor tag works as follows:
一个典型的锚标签的工作原理如下:
a href link tag is written like so:
href 链接标签是这样写的:
<a href="anchor_example2.html#a001">Jump to a001</a>
See the #a001 above? That is referencing an id in the html page, it will jump to it if you click this link.
看到上面的#a001了吗?即在 html 页面中引用一个 id,如果您单击此链接,它将跳转到它。
To provide an example of how this id that we would jump to might look in a page look below.
提供一个示例,说明我们将跳转到的此 id 在下面的页面中的外观。
<li id="a001">text here</li>