<body>
<h1>Welcome to the HTML Links Lab</h1>
<p>
This page will be used to practice creating different kinds of HTML links.
</p>
<nav>
<!-- Navigation links will be added here -->
</nav>
<div class="section">
<h2>Section 1</h2>
<p>
<a href="https://labex.io">Visit LabEx</a>
</p>
</div>
<div class="section">
<h2>Section 2</h2>
<p>
This is the section we will link to from the top of the page. It is placed
far down to demonstrate the page jump effect.
</p>
</div>
</body>
在上一步中,你创建了一个指向 #section2 的链接。现在,你需要为该链接创建目标。这可以通过使用 id 属性来完成。
id 属性为页面上的 HTML 元素提供了一个唯一的标识符。id 的值在 HTML 文档中必须是唯一的。我们的内部链接 href="#section2" 将会查找一个具有 id="section2" 的元素。
让我们为 Section 2 的 <h2> 标签添加这个 id。找到以下行:
<h2>Section 2</h2>
并修改它以包含 id 属性:
<h2 id="section2">Section 2</h2>
你的 index.html 文件第二个部分的更新内容现在应该看起来像这样:
<div class="section">
<h2 id="section2">Section 2</h2>
<p>
This is the section we will link to from the top of the page. It is placed
far down to demonstrate the page jump effect.
</p>
</div>
保存文件并返回“Web 8080”标签页。点击页面顶部的“Jump to Section 2”链接。浏览器现在应该平滑地滚动到“Section 2”标题。