为什么HTML中不允许使用重复的ID [英] Why are duplicate ids not allowed in HTML

查看:170
本文介绍了为什么HTML中不允许使用重复的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML页面在什么情况下非法包含具有重复ID属性的元素?

Under what circumstances is it illegal for an HTML page to contain elements with duplicate ID attributes?

作为与之合作的开发人员多年的HTML,我知道的意图是元素ids 应该是唯一的 - 我要问的是重复ID的实际负面影响。

As a developer who has worked with HTML for many years, I am aware that the intention is that element ids should be unique - what I am asking is for the practical negative effects of duplicate ids.

当然,getElementByID() - 类似于某些库中的函数可能会返回数组而不是单个元素,这可能会在开发人员没有预料到这种情况时引发问题。但是,据我所知,这些函数将继续如此清晰地运行,它们不是id重复的突破效果。

Granted, getElementByID()-like functions in some libraries might return arrays rather than a single element and this could cause issues when the developer had not anticipated this case. However, as far as I know, such functions will continue to operate so clearly they are not a breaking-effect of id duplicates.

那么为什么重复的id是据说不是允许

So why is it that duplicate ids are said to be not allowed?

编辑:问题的驱动程序是我在生成列表/重复项目时看到一些模板库,生成具有重复ID的元素,我想知道它在实际条件下可能产生的影响以及如何决定是否采用这些库。

The driver for the question was that I saw some templating libraries when generating list/repeated items, producing elements with duplicate ids and I wondered what the impact of that might be in practical terms and how to decide whether to adopt those libraries.

我也想知道模态的影响插件或任何其他可能克隆现有隐藏节点从而通过代码创建重复的插件,然后在这种情况下浏览器将执行的操作。

I also wondered about the effect of modal plugins, or any other, that might clone an existing hidden node and thereby create a duplicate via code, and then what the browser would do in that case.

推荐答案


规格说明UNIQUE

HTML 4.01规范说ID必须是文档范围内唯一的。

HTML 4.01 specification says ID must be document-wide unique.

HTML 5规范说同样的话,但换句话说。
表示ID必须在其主要子树中是唯一的,如果我们。 home-subtreerel =nofollow noreferrer>阅读它的定义。

HTML 5 specification says the same thing but in other words. It says that ID must be unique in its home subtree, which is basically the document if we read the definition of it.

避免重复

但是,由于HTML渲染器在HTML
渲染时非常宽容,因此它们允许重复ID。如果在JavaScript中以编程方式访问
元素时,应该可以避免这种情况,并且严格避免。我不确定 getElementById
函数应该在找到多个匹配元素时返回?
应该:

But since HTML renderers are very forgiving when it comes to HTML rendering they permit duplicate IDs. This should be avoided if at all possible and strictly avoided when programmatically accessing elements by IDs in JavaScript. I'm not sure what getElementById function should return when several matching elements are found? Should it:


  • 返回错误?

  • 返回第一个匹配元素?

  • 返回最后一个匹配元素?

  • 返回一组匹配元素?

  • 什么都不返回?

  • return an error?
  • return first matching element?
  • return last matching element?
  • return a set of matching elements?
  • return nothing?

但即使浏览器这些天工作可靠,也没有人可以保证
将来这种行为,因为这违反了规范。
这就是为什么我建议你永远不要在同一个
文件中复制ID。

But even if browsers work reliably these days, nobody can guarantee this behavior in the future since this is against specification. That's why I recommend you never duplicate IDs within the same document.

这是一个答案< a href =https://softwareengineering.stackexchange.com/users/1894/robert-koritnik> Robert Koritnik 软件工程 danludwig

问题:两个具有相同id属性的HTML元素:它真的有多糟糕?

重复ID不允许使用HTML


该代码不正确。不正确的不是灰色阴影。这个
代码违反了标准,因此不正确。它将失败
验证检查,它应该。也就是说,目前市场上没有任何浏览器会以b $ b为单位抱怨它,或者在
全部都有问题。浏览器将在他们的权利范围内 o抱怨它,但
目前没有任何当前版本。哪个
并不意味着未来版本可能不会严重处理此代码。

That code is incorrect. Incorrect doesn't come in shades of grey. This code violates the standard and is therefore incorrect. It would fail validation checking, and it should. That said, no browser currently on the market would complain about it, or have any problem with it at all. Browsers would be within their rights o complain about it, but none of the current versions of any of them currently do. Which doesn't mean future versions might not treat this code badly.

〜来自丹雷

重复的ID和JavaScript

因此,如果您在HTML中使用重复的ID,则许多库将无法按预期工作。大多数库将获得他们找到的第一个id并返回该元素。当我们查看纯JavaScript时: document.getElementById(idName); 应该返回多个具有相同id的元素。它说它必须按树顺序返回第一个元素。

So if you use duplicate ids in your HTML many libraries will not work as expected. The most libraries will get the first id they find and return that element. When we look at pure JavaScript: the document.getElementById("idName"); should return in the case of multiple elements with the same id. It says it must return the first element, in tree order.

这篇关于为什么HTML中不允许使用重复的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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