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

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

问题描述

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

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

作为一名使用 HTML 多年的开发人员,我知道其意图是元素 id 应该 是唯一的 - 我要问的是重复 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 在它的主子树中必须是唯一的,这基本上是如果我们阅读它的定义

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 中按 ID 排列的元素.我不确定什么 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:

  • 返回错误?
  • 返回第一个匹配的元素?
  • 返回最后一个匹配的元素?
  • 返回一组匹配的元素?
  • 什么都不返回?

但即使现在浏览器运行可靠,也没有人能保证将来会出现这种行为,因为这违反了规范.这就是为什么我建议您永远不要在同一个 ID 中重复 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.

这是 Robert Koritnik软件工程danludwig
问题:两个 HTML 元素相同的 id 属性:它到底有多糟糕?

HTML 中不允许重复的 id

该代码不正确.不正确的不是灰色阴影.这代码违反了标准,因此是不正确的.它会失败验证检查,它应该.也就是说,目前没有浏览器市场上会抱怨它,或有任何问题在全部.浏览器在他们的权利范围内 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天全站免登陆