jQuery ID 在整个页面中必须是唯一的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9454645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does ID have to be unique in the whole page?
提问by good_evening
I am using jQuery and I am just wondering, does ID have to be always unique in the whole page? Class, I know, can be repeated as many times as you like, what about ID?
我正在使用 jQuery,我只是想知道,ID 是否必须在整个页面中始终唯一?类,我知道,可以重复多次,ID呢?
采纳答案by FatalError
Yes, it must be unique.
是的,它必须是唯一的。
HTML4:
HTML4:
https://www.w3.org/TR/html4/struct/global.html#h-7.5.2
https://www.w3.org/TR/html4/struct/global.html#h-7.5.2
Section 7.5.2:
第 7.5.2 节:
id = name [CS] This attribute assigns a name to an element. This name must be unique in a document.
id = name [CS] 此属性为元素分配名称。此名称在文档中必须是唯一的。
HTML5:
HTML5:
https://www.w3.org/TR/html5/dom.html#element-attrdef-global-id
https://www.w3.org/TR/html5/dom.html#element-attrdef-global-id
The id attribute specifies its element's unique identifier (ID). The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.
id 属性指定其元素的唯一标识符 (ID)。该值在元素的主子树中的所有 ID 中必须是唯一的,并且必须至少包含一个字符。该值不得包含任何空格字符。
回答by Royi Namir
from mdn https://developer.mozilla.org/en/DOM/element.id
来自 mdn https://developer.mozilla.org/en/DOM/element.id
so i guess it better be...
所以我想最好是...
回答by PandaWood
Technically, by HTML5 standards ID must be unique on the page - https://developer.mozilla.org/en/DOM/element.id
从技术上讲,根据 HTML5 标准 ID 在页面上必须是唯一的 - https://developer.mozilla.org/en/DOM/element.id
But I've worked on extremely modular websites, where this is completely ignored and it works. And it makes sense - the most surprising part.
但是我曾在极其模块化的网站上工作过,在那里这完全被忽略并且可以工作。这是有道理的——最令人惊讶的部分。
We call it "componentization"
我们称之为“组件化”
For example, you might have a component on your page, which is some kind of widget. It has stuff inside with their own unique IDs eg "ok-button"
例如,您的页面上可能有一个组件,它是某种小部件。它里面有自己独特的 ID 的东西,例如“确定按钮”
Once there are many of these widgets on the page, you technically have invalid HTML. But it makes perfect sense to componentizeyour widgets so that they each, internally, reference their own ok button eg if using jquery to search from it's own root it might be: $widgetRoot.find("#ok-button")
一旦页面上有许多这些小部件,从技术上讲,您的 HTML 是无效的。但是将您的小部件组件化是非常有意义的,以便它们每个在内部都引用自己的 ok 按钮,例如,如果使用 jquery 从它自己的根进行搜索,它可能是:$widgetRoot.find("#ok-button")
This works for us, though technically IDs shouldn't be used at all, once they're not unique.
这对我们有用,但从技术上讲,一旦它们不是唯一的,就不应该使用 ID。
As cited above, even YouTube does it, so it's not so renegade.
如上所述,即使是 YouTube 也这样做了,所以它并没有那么叛逆。
回答by Константин Ван
Does an ID have to be unique in the whole page?
ID在整个页面中必须是唯一的吗?
No.
不。
Because the HTML Living Standardof December 6, 2019,clearly states:
因为2019 年 12 月 6 日的HTML Living Standard明确指出:
The
class
,id
, andslot
attributes may be specified on all HTML elements. ……. When specified on HTML elements, theid
attribute value must be unique amongst all the IDs in the element's treeand must contain at least one character.
的
class
,id
和slot
属性可以在所有的HTML元素指定。……. 在 HTML 元素上指定时,id
属性值在元素树中的所有 ID 中必须是唯一的,并且必须至少包含一个字符。
and a page may have several DOM trees. It does, for example, when you've attached (Element.attachShadow()
) a shadow DOM treeto an element.
和一个页面可以有多个DOM树。例如,当您将 ( Element.attachShadow()
) 一个shadow DOM 树附加到一个元素时。
TL; DR
TL; DR
Does an ID have to be unique in the whole page?
ID在整个页面中必须是唯一的吗?
No.
不。
Does an ID have to be unique in a DOM tree?
ID在 DOM 树中必须是唯一的吗?
Yes.
是的。
回答by vanduc1102
回答by cjtech
Browsers used to be lenient on this (many years ago when css was young) and allow the ID to be used more than once. They have become more strict.
浏览器过去对此很宽容(很多年前当 css 还很年轻时)并允许多次使用 ID。他们变得更加严格。
However, yes ID's are to be unique and only used once.
但是,是的 ID 必须是唯一的,并且只能使用一次。
If you need to use css formatting more than once use CLASS.
如果您需要多次使用 css 格式,请使用 CLASS。
回答by Bram Vanroy
That's basically the whole point of an ID. :) IDs are specific, can only be used once per page. Classes can be used as pleased.
这基本上就是 ID 的全部意义所在。:) ID 是特定的,每页只能使用一次。可以随意使用类。
回答by keune
With Javascript, you can only reference to one element using ID. document.getElementById
and jQuery's $
selector will return only the first element matching. So it doesn't make sense using the same ID on multiple elements.
使用 Javascript,您只能使用 ID 引用一个元素。document.getElementById
并且 jQuery 的$
选择器将只返回匹配的第一个元素。所以在多个元素上使用相同的 ID 是没有意义的。
回答by keune
IDs always have to be unique.
ID 必须始终是唯一的。
Everybody has a uniqueidentification number (ex. Social Security number), and there are lots of people in a social class
每个人都有一个唯一的身号码(例如社会安全号码),并且在一个社会阶层中有很多人
回答by Michele Spagnuolo
Yes, IDs are unique. Class are not.
是的,ID 是唯一的。类不是。