Should I make HTML Anchors with #39;name#39; or #39;id#39;?(我应该用name还是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?
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.
- Parse the URL, and let fragid be the <fragment> component of the URL.
- If fragid is the empty string, then the indicated part of the document is the top of the document.
- 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.
- 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.
- 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.
这篇关于我应该用'name'还是'id'制作HTML锚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我应该用'name'还是'id'制作HTML锚?
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01