gpt4 book ai didi

html - 我应该使用 'name' 还是 'id' 制作 HTML anchor ?

转载 作者:bug小助手 更新时间:2023-10-28 10:53:06 24 4
gpt4 key购买 nike

当人们想用“http://example.com/#foo”方法引用网页的某个部分时,应该使用

<h1><a name="foo"/>Foo Title</h1>

<h1 id="foo">Foo Title</h1>

它们都有效,但它们是平等的,还是它们有语义上的差异?

最佳答案

根据 HTML 5 规范,5.9.8 Navigating to a fragment identifier :

For HTML documents (and the text/html MIME type), the following processing model must be followed to determine what the indicated part of the document is.

  1. Parse the URL, and let fragid be the <fragment> component of the URL.
  2. If fragid is the empty string, then the indicated part of the document is the top of the document.
  3. If there is an element in the DOM that has an ID exactly equal to fragid, then the first such element in tree order is the indicated part of the document; stop the algorithm here.
  4. If there is an a element in the DOM that has a name attribute whose value is exactly equal to fragid, then the first such element in tree order is the indicated part of the document; stop the algorithm here.
  5. Otherwise, there is no indicated part of the document.

所以,它会寻找id="foo",然后会跟着name="foo"

编辑:正如@hsivonen 所指出的,在 HTML5 中 a 元素没有 name 属性。但是,上述规则仍然适用于其他命名元素。

关于html - 我应该使用 'name' 还是 'id' 制作 HTML anchor ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/484719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com