简体   繁体   English

为什么嵌套的锚标签是非法的?

[英]Why are nested anchor tags illegal?

I learned that nesting anchor tags is not standards compliant HTML.我了解到嵌套锚标记不是符合标准的 HTML。

From W3:从 W3:

Links and anchors defined by the A element must not be nested; A 元素定义的链接和锚点不得嵌套; an A element must not contain any other A elements. A 元素不得包含任何其他 A 元素。

Since the DTD defines the LINK element to be empty, LINK elements may not be nested either.由于 DTD 将 LINK 元素定义为空,因此也不能嵌套 LINK 元素。

It would seem like alternatives such as those suggested in the selected answer in this question would have more of a chance of creating unexpected behavior than simply nesting the anchors would!看起来像在这个问题的所选答案中建议的替代方案比简单地嵌套锚点更有可能产生意想不到的行为!

It also seems like overkill to make onclick event handlers just to redirect the page in JS.制作 onclick 事件处理程序只是为了在 JS 中重定向页面似乎也有些矫枉过正。 Not to mention using a script solution would cause problems for users browsing with scripts disabled.更不用说使用脚本解决方案会给禁用脚本的用户浏览带来问题。

EDIT编辑

What is interesting was that I was working on a fiddle to demonstrate and I had overlooked that chrome was actually restructuring the DOM as such:有趣的是,我正在制作一个小提琴来演示,但我忽略了 chrome 实际上是这样重构 DOM 的:

<div id="container">

    <a href="http://yahoo.com"></a>
    <div class="parent">
        <a href="http://yahoo.com">Parent Element</a>
        <a href="http://google.com">
            <div class="child">Child Element</div>
        </a>
        <a href="http://bing.com">
            <div class="child">Other Child</div>
        </a>
    </div>

</div>

I overlooked this because I saw the hover working and had my mouse on the text.我忽略了这一点,因为我看到悬停工作并且将鼠标放在文本上。 Knowing this now doesn't necessarily change my question, but it sure does illustrate that it doesn't even work the way I thought.现在知道这一点并不一定会改变我的问题,但它确实说明它甚至不像我想的那样工作。

Keep in mind that an anchor isn't just a link, it's also something to which one can link.请记住,锚点不仅仅是一个链接,它也是一个可以链接到的东西。 (Though the former use is far more common than the latter.) Quoting W3C (old, but relevant): (虽然前者使用的是远远比后者更常见。)报价W3C (老了,但相关的):

An anchor is a piece of text which marks the beginning and/or the end of a hypertext link.锚点是标记超文本链接开始和/或结束的一段文本。

To that end, don't think of an anchor as a link.为此,不要将锚点视为链接。 Think of it as a point in the document which connects to (and/or from) another point (in the same document or another document, it makes no difference).将其视为连接到(和/或来自)另一个点的文档中的一个点(在同一文档或另一个文档中,它没有区别)。 Two points in some non-physical space which are connected by a non-physical thread.一些非物理空间中的两个点由非物理线程连接。

Given that, anchors shouldn't contain a lot of content.鉴于此,锚点不应包含大量内容。 If it contains a lot of content, it ceases to become a "point" and starts to become an "area."如果它包含很多内容,它就不再是一个“点”而开始成为一个“区域”。 For example, imagine an anchor which when rendered takes up more space than the browser can display at once.例如,想象一个锚点,它在渲染时占用的空间比浏览器一次可以显示的空间还多。 If something links to that anchor, where should it go?如果有东西链接到那个锚点,它应该去哪里? The beginning?开始? Middle?中间? End?结尾? (Intuitively you might think the beginning, but you get the idea.) (直觉上你可能会想到开始,但你明白了。)

Furthermore, anchors definitely shouldn't contain other anchors.此外,锚绝对不应该包含其他锚。 The non-physical connections between the non-physical points can become ambiguous.非物理点之间的非物理连接可能变得不明确。 Does the child anchor connect to the other point, or does the parent anchor connect to the other point?子锚点连接到另一个点,还是父锚点连接到另一个点? This probably doesn't result in being a big deal in most cases, since the vast majority of anchors today are one-way links from the anchor to another document.在大多数情况下,这可能不会造成什么大问题,因为当今绝大多数锚点都是从锚点到另一个文档的单向链接。 But the purpose of the anchor is more than just a one-way link to another document, so the definition continues to embody that purpose.但是锚点的目的不仅仅是指向另一个文档的单向链接,因此定义继续体现该目的。

The spec for <a> has a content model of: <a>规范具有以下内容模型:

Transparent, but there must be no interactive content descendant.透明,但必须没有交互式内容后代。

So the spec is actually more complicated than you say.所以规范实际上比你说的要复杂。 This also applies to <button> -- essentially you can't have working links inside of links or buttons inside of buttons.这也适用于<button> —— 本质上,你不能在链接内有工作链接或按钮内有按钮。

Unfortunately I don't have a strong answer to your question why -- I can only speculate.不幸的是,我对你的问题为什么没有一个强有力的答案——我只能推测。 One clear reason is the ambiguity that it causes (eg which anchor should be followed when the inner one is clicked?)一个明显的原因是它引起的歧义(例如,单击内部锚点时应遵循哪个锚点?)

This creates not only functional ambiguity, but also semantic ambiguity.这不仅会造成功能上的歧义,还会造成语义上的歧义。 The content of the <a> is a label for its hyperlink. <a>的内容是其超链接的标签。 So does this mean that the inner hyperlink is part of the label for the content of the outer link?那么这是否意味着内部超链接是外部链接内容标签的一部分?

While the other good people of the community gave excellent answer, I would like to contribute by giving an example of how to mimic nested anchor tags without actually nesting them in the markup , by making them appear visually as if one is inside another's area:虽然社区的其他好人给出了很好的答案,但我想通过举例说明如何模仿嵌套的锚标记而不将它们实际嵌套在标记中,使它们在视觉上看起来好像一个人在另一个区域内:

 .parent{ position: relative; width: 300px; height: 150px; border: 1px solid salmon; } .parent .main-link{ display: block; margin-bottom: 2em; } /* expands the clickable area of the main link to fill the parent container, because it's the nearest ancestor with "position:relative" */ .parent .main-link::before{ content: ''; position: absolute; top: 0; right: 0; bottom: 0; left: 0; } .parent a:hover{ color: green; } /* bring other links "forward" to make them clickable */ .parent .secondary-link{ position: relative; z-index: 1; }
 <div class='parent'> <a href='#111' class='main-link'>main link, full area</a> <a href='#222'class='secondary-link'>secondary link</a> </div>

Anchor nesting is not recommended by standards (although all browsers I tested it on seem to lead to the innermost click target) because of ambiguity.由于模棱两可,标准不推荐锚嵌套(尽管我测试过的所有浏览器似乎都会导致最里面的点击目标)。

It is not logic to say that a big part should lead to a certain page and in this part a small section should lead somewhere else.说大部分应该通向某个页面,而在这一部分中一小部分应该通向其他地方,这是不合逻辑的。 I still have to see any use case for this kind of behaviour.我仍然需要看到这种行为的任何用例。 With the evolution of available scripting languages, this kind of iteration or heritage can work (javascript click events on divs for exemple), but never will a code want to direct to a certain URL and then to another (parent anchors' href).随着可用脚本语言的发展,这种迭代或继承可以工作(例如 div 上的 JavaScript 点击事件),但代码永远不会想要指向某个 URL,然后指向另一个(父锚点的 href)。

Edit: Looking at the question you referenced in yours, stacking 2 anchors that point to the exact same href is completely useless.编辑:查看您在您的问题中引用的问题,堆叠 2 个指向完全相同 href 的锚点是完全没用的。 Just remove the nested one and everything is gonna be good.只需删除嵌套的,一切都会好起来的。 If it is because you did a CSS rule like aa{color:red;} use a span!如果是因为您执行了像aa{color:red;}这样的 CSS 规则aa{color:red;}使用跨度! Or even a div with display:inline but please, pretty please, an anchor should go somewhere, should not be used just for styling purpose or something.或者甚至是一个带有display:inline的 div 但拜托,非常拜托,一个锚应该放在某个地方,不应该仅仅用于样式目的或其他东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM