如何在网页中进行链接?

时间:2016-01-22 17:27:40

标签: html html5

我知道链接一般看起来像

<a href="examplesite.com"> examplesite.com  </a>

我想知道有人会如何在页面中自行链接。有点像当有人点击维基百科中的传记部分时,它会向下滚动到有传记的部分但保持在同一页面上。

任何一个例子都可能很棒。

3 个答案:

答案 0 :(得分:3)

我相信你指的是网址片段(例如a.k.a.命名锚点或书签链接)。

您可以创建如下链接:

<a href="#example">Jump to example</a>

这会将您带到页面中ID为example的元素所在的部分。像:

<h1 id="example">example</h1>

older versions of HTML中,首先使用name属性,但ID已替换为。

答案 1 :(得分:0)

您发布的内容实际上是网站内的链接。它不包含诸如http://之类的协议,也不以//开头,这将指示协议相对链接,因此它将相对于您当前所在的任何路径加载exampleside.com

这些是您可以使用的链接(每个链接都在href="..."内)。我们假设您目前在http://example.net/foo/index.html

  • https://example.com - 转到&#34;外部&#34;网站https://example.com
  • //example.com - 转到&#34;外部&#34;网站xxx://example.com xxx是用于加载example.net的协议,因此在我的示例中http://example.com
  • www.example.com - 转到http://example.net/foo/www.example.com,因为它不是外部链接
  • #foo - 转到当前网站上id="foo"的元素(不从服务器加载任何内容)

所以你想要的可能是最后一个例子:<a href="#foo">...</a>然后id="foo"你要跳转到的元素。

答案 2 :(得分:0)

向要链接的元素添加一些id,例如

<div id="target">Hello</div>

然后,您可以使用#

链接它
<a href="#target">Go to target</a>

<a href="#target">Go to target</a>
<hr style="height: 300vh" />
<div id="target">Hello</div>