如何避免重复的XHTML锚点?
我做了一个页面并使用一个名为 WebKing 的软件对其进行了检查,它告诉我我有重复的锚点?
<ul >
<li><a href="#toc1" title="About The Code">About The Code</a></li>
<li><a href="#toc2" title="Blah Blah Test">Link 2 is boring</a></li>
<li><a href="#toc3" title="3rd link test">3rd line in</a></li>
<li><a href="#toc4" title="Contact Manny">Contact Manny</a></li>
<h3><a name="toc1" id="toc1">About the code</a></h3>
<h3><a name="toc2" id="toc2">Link 2 test</a></h3>
<h3><a name="toc3" id="toc3">3rd test</a></h3>
<h3><a name="toc4" id="toc4">Contact Manny</a></h3>
</ul>
那么我做错了什么?我要把id改成别的吗?
I did a page and did a check on it using a software called WebKing and it tells me I have duplicate anchors??
<ul >
<li><a href="#toc1" title="About The Code">About The Code</a></li>
<li><a href="#toc2" title="Blah Blah Test">Link 2 is boring</a></li>
<li><a href="#toc3" title="3rd link test">3rd line in</a></li>
<li><a href="#toc4" title="Contact Manny">Contact Manny</a></li>
<h3><a name="toc1" id="toc1">About the code</a></h3>
<h3><a name="toc2" id="toc2">Link 2 test</a></h3>
<h3><a name="toc3" id="toc3">3rd test</a></h3>
<h3><a name="toc4" id="toc4">Contact Manny</a></h3>
</ul>
So what am I doing wrong?? Do I change the id to something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这看起来只是您正在使用的分析软件中的一些不好的启发式方法。该代码在技术上没有任何问题。
也就是说,现代方法(即不迎合 Netscape 4)会说:
……而且我怀疑这也将避免触发上述不良启发式。
你或许也应该有更好的 id。
id="about"
— 告诉读者他们去了哪里的 URL 通常比不告诉读者去向的 URL 更好,因此/mypage/#about
优于/mypage/# toc1
关于不良样式的主题,标题属性用于提供有关元素的建议信息。它应该包含有用的额外信息。您的示例复制了链接的正文。最好的情况下,这只是需要下载一些额外的字节。在最坏的情况下,您可以预期一些屏幕阅读器用户必须聆听重复的每个链接的目的地。
This looks like it is just some bad heuristics in the analysis software you are using. There isn't anything technically wrong with that code.
That said, a modern approach (i.e. not pandering to Netscape 4) would be to say:
… and I suspect this would also avoid triggering the aforementioned bad heuristic.
You should probably have better ids too.
id="about"
— URLs that inform readers where they go are generally better than URLs that don't so/mypage/#about
beats/mypage/#toc1
On the subject of bad style, the title attribute is there to provide advisory information about an element. It should contain helpful extra information. Your example has it duplicating the main text of the links. At best this will just be some extra bytes to download. At worst, you can expect some screen reader users to have to listen to the destination of every link being repeated.
name 和 id 属性共享相同的命名空间,因此它们需要不同。
http://www.w3.org/TR/html401 /struct/links.html#h-12.2.3
如果您正在编写有效的 XHTML,请尽量不要使用名称标签。
http://www.w3.org/TR/xhtml1/
第 4.10 节。具有“id”和“name”属性的元素
请注意,在 XHTML 1.0 中,这些元素的 name 属性已正式弃用,并将在 XHTML 的后续版本中删除。
此外,您还有一些不在 li 内但在 a 内的 h3 ul。
The name and id attribute share the same namespace so they need to be different.
http://www.w3.org/TR/html401/struct/links.html#h-12.2.3
If you are writing valid XHTML try not to use the name tag.
http://www.w3.org/TR/xhtml1/
Section 4.10. The elements with 'id' and 'name' attributes
Note that in XHTML 1.0, the name attribute of these elements is formally deprecated, and will be removed in a subsequent version of XHTML.
Also you have some h3's that aren't inside li's but are inside of a ul.