如何设置页面中特定部分的锚链接?

Mis*_*erP 1 html

我有一个页面,让我们说第2页,填写了几个<section>:

<section class="box special" id="section1">
 //
</section>
<section class="box special" id="section2">
 //
</section>
<section class="box special" id="section3">
 //
</section>
<section class="box special" id="section4" style="background:#3399ff;">
 //
</section>
Run Code Online (Sandbox Code Playgroud)

然后我有另一页,page1,我想在其中放置一个锚链接

    <ul class="actions">
        <li><a href="page2.php" class="button">Link to page2</a></li> 
   </ul>
Run Code Online (Sandbox Code Playgroud)

我想要实现的是设置这个锚点链接,以便在第2页中找到特定<section>的,而不仅仅是在第2页的顶部.那可能吗?如果是这样,怎么样?

Sum*_*ans 6

使用您要链接到的部分的id值#{id}在哪里{id}.例如

<a href="page2.php#section1" class="button">Link to page2</a>
Run Code Online (Sandbox Code Playgroud)

会带你去section1 sectionpage2.


进一步阅读:

MDN href属性文档.


Sha*_*son 5

使用定位标记

<a name="section1"><section ...></section></a>
Run Code Online (Sandbox Code Playgroud)

然后在链接中添加特定锚点

<a href="page2.php#section1">Link</a>
Run Code Online (Sandbox Code Playgroud)

就像评论所建议的那样,如果您使用的是HTML5(看起来确实如此),则可以删除定位标记,而只需使用section元素的id

<section id="section1">...
Run Code Online (Sandbox Code Playgroud)

  • 注意-在html5中,不再需要&lt;a name=""&gt;`,因为它可以通过http://stackoverflow.com/questions/484719/html-中的&lt;section&gt;中的id属性来实现。锚定名称或编号 (2认同)