URL“片段标识符” HTML文档的语义 [英] URL "fragment identifier" semantics for HTML documents

查看:238
本文介绍了URL“片段标识符” HTML文档的语义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用MoinMoinwiki软件的新安装。当我在玩它时,大部分是随机测试页面,我创建了一个带有片段的链接。

  blah blah另请参阅[ [SomeStuff#whatever | some something about about]] 

然后我需要弄清楚如何创建该无论片段标识符的锚点。我不记得要用MediaWiki做这件事,所以我不得不四处挖掘,但最后我发现MoinMoin有一个锚宏:

  ==无论== 
<< Anchor(whatever)>>

查看生成的HTML,我惊讶地发现一个空的< span> 标签的id值为whatever。我预计它会是一个属性为whatever的< a> 标签。我挖了一遍,找到了源代码,并且有一条评论说,他们将它从< a> 标记进行了更改,以避免< pre> 部分。这使我感到困惑—不是因为IE的缘故,而是因为它看起来好像他们的修复让整个锚定机制完全破裂。然而,令我惊讶的是,进一步的测试表明它运行良好。我写了一个带有id值的300 < span> 标签的测试页面,当Firefox的行为与我预期的完全一致时,我进一步震惊了自己使用< a> 标签。当我将所有< span> 标签更改为< em>

$时, b
$ b

所以到目前为止,你可能会和我一样惊讶,否则你就会想:愚蠢的人怎么能有这么多的声望点?如果你处于第二类,那么我真的已经在HTML中输入了大约15年的时间了 - mdash;一个很多的HTML—它不知何故逃过了我的注意,浏览器使用HTML片段来查找任何与匹配的id匹配的元素?



mind status:blown

解决方案

Well HTML 4.01非常清晰...

  12.2.3具有id属性的锚点

id属性可用于在任何元素的开始标记处创建锚点
(包括A元素)。

此示例说明如何使用id属性将锚点放置在
H2元素中。锚点通过A元素链接到。

您可以在< A href =#section2>第二部分< / A>中阅读更多相关信息。
...稍后在文件
< H2 id =section2> Section Two< / H2>
...稍后在文件
< P>中请参阅< A href =#section2> Section Two< / A>在
以上获取更多细节。

如果有任何安慰,你并不孤单。最近有人评论我的另一个答案,说同样的事情。



它从HTML 4开始,所以如果你在1998/9之前学过你的HTML,与其他变化相比,并不是一个主要的因素,因此不是最容易注意的事情。我直到2001年才开始学习HTML,所以HTML 4是我开始学习的地方。


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 <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.

这篇关于URL“片段标识符” HTML文档的语义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆