HTML之给网页增加锚点

[size=medium][b]HTML Anchors with 'name' or 'id'?[/b][/size]

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, [url=http://www.w3.org/html/wg/drafts/html/master/browsers.html#scroll-to-fragid]5.9.8 Navigating to a fragment identifier[/url]:

[quote]
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.
[/quote]

[b]So, it will look for id="foo", and then will follow to name="foo"[/b]


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.

-----------------------------------------------------------------------------


html-anchors-with-name-or-id

http://stackoverflow.com/questions/484719


-
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值