Didn"t forge 2009-01-27 18:57 采纳率: 25%
浏览 346
已采纳

我应该使用'名称'或'id'的 HTML 锚?

When one wants to refer to some part of a webpage with the "http://example.com/#foo" method, should one use

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

or

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

They both work, but are they equal, or do they have semantic differences?

转载于:https://stackoverflow.com/questions/484719/should-i-make-html-anchors-with-name-or-id

  • 写回答

14条回答 默认 最新

  • MAO-EYE 2009-01-27 19:10
    关注

    According to the HTML 5 specification, 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.

    So, it will look for id="foo", and then will follow to name="foo"

    Edit: As pointed out by @hsivonen, in HTML5 the a element has no name attribute. However, the above rules still apply to other named elements.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(13条)

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题