开发者

URL "fragment identifier" semantics for HTML documents

I've been working with a new installation of the "MoinMoin" wiki software. As I was playing with it, typing in mostly random test pages, I created a link with a fragment

blah blah see also [[SomeStuff#whatever|some other stuff about whatever]]

Then I needed to figure out how to create the anchor for that "whatever" fragment identifier. I don't recall having to do that with MediaWiki, so I had to dig around, but finally I found that MoinMoin has an "Anchor" macro:

== Whatever ==
<<Anchor(whatever)>>

Looking at the generated HTML, I was surprised to see an empty <span> tag with an "id" value of "whatever". I expected that it'd be an <a> tag with a "name" attribute of "whatever". I dug around and found the source, and there's a comment that says they changed it from an <a> tag in order to avoid some IE problem with <pre> sections. This confused me — not because of the IE thing, but because it looked to me as if their "fix" had left the whole anchor mechanism completely broken.

Much to my surprise, however, further testing indicated that it worked fine. I wrote a test page with 300 <span> tags all with "id" values, and I further shocked myself when Firefox behaved exactly as I would have expected it to had I used <a> tags. It also worked when I changed all the <span> tags to开发者_StackOverflow中文版 <em>.

So by this time, you're either as surprised as I was, or else you're thinking "how can somebody that dumb have so many reputation points?" If you're in the second category, is it really the case that I've been typing in HTML for about 15 years now — a lot of HTML — and it's somehow escaped my notice that browsers use the HTML fragment to find any sort of element with a matching "id"?

mind status: blown


Well HTML 4.01 is pretty clear ...

12.2.3 Anchors with the id attribute

The id attribute may be used to create an anchor at the start tag of any element
(including the A element).

This example illustrates the use of the id attribute to position an anchor in an 
H2 element. The anchor is linked to via the A element.

You may read more about this in <A href="#section2">Section Two</A>.
...later in the document
<H2 id="section2">Section Two</H2>
...later in the document
<P>Please refer to <A href="#section2">Section Two</A> above
for more details.

If it's any consolation, you're not alone. Somebody commented on another answer of mine on SO recently, saying the same thing.

It started with HTML 4, so if you learnt your HTML before 1998/9, the change was not a major one compared to the other changes and therefore not the easiest thing to notice. I didn't really start to learn HTML until 2001, so HTML 4 was where I began.


I didn't know this either. It's official, though. From the HTML 4.01 spec:

Destination anchors in HTML documents may be specified either by the A element (naming it with the name attribute), or by any other element (naming with the id attribute).

Wikipedia knew it too:

In HTML applications, http://www.foo.org/foo.html#bar refers to the element with the id attribute that has the value bar (i.e., id="bar") in the document identified by the URI http://www.foo.org/foo.html, which is typically the location from which the document would be obtained via the Internet. The deprecated name attribute can also be used for this purpose in the same manner.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜