Html 一个html元素可以有多个id吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/192048/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:31:02  来源:igfitidea点击:

Can an html element have multiple ids?

htmlxhtmlstandards-compliance

提问by webmat

I understand that an id must be unique within an HTML/XHTML page.

我知道 id 在 HTML/XHTML 页面中必须是唯一的。

My question is, for a given element, can I assign multiple ids to it?

我的问题是,对于给定的元素,我可以为其分配多个 id 吗?

<div id="nested_element_123 task_123"></div>

I realize I have an easy solution with simply using a class. I'm just curious about using ids in this manner.

我意识到我有一个简单的解决方案,只需使用一个类。我只是对以这种方式使用 id 感到好奇。

采纳答案by timmow

No. From the XHTML 1.0 Spec

不。来自XHTML 1.0 规范

In XML, fragment identifiers are of type ID, and there can only be a single attribute of type ID per element. Therefore, in XHTML 1.0 the id attribute is defined to be of type ID. In order to ensure that XHTML 1.0 documents are well-structured XML documents, XHTML 1.0 documents MUST use the id attribute when defining fragment identifiers on the elements listed above. See the HTML Compatibility Guidelines for information on ensuring such anchors are backward compatible when serving XHTML documents as media type text/html.

在 XML 中,片段标识符属于 ID 类型,并且每个元素只能有一个 ID 类型的属性。因此,在 XHTML 1.0 中,id 属性被定义为 ID 类型。为了确保 XHTML 1.0 文档是结构良好的 XML 文档,XHTML 1.0 文档必须在上面列出的元素上定义片段标识符时使用 id 属性。有关在将 XHTML 文档作为媒体类型 text/html 提供时确保此类锚点向后兼容的信息,请参阅 HTML 兼容性指南。

回答by user123444555621

Contrary to what everyone else said, the correct answer is YES

与其他人所说的相反,正确答案是YES

The Selectors specis very clear about this:

选择器的规格是非常清楚这一点:

If an element has multiple ID attributes, all of them must be treated as IDs for that element for the purposes of the ID selector.Such a situation could be reached using mixtures of xml:id, DOM3 Core, XML DTDs, and namespace-specific knowledge.

如果一个元素有多个 ID 属性,则为了 ID 选择器的目的,所有这些属性都必须被视为该元素的 ID。可以使用 xml:id、DOM3 Core、XML DTD 和特定于命名空间的混合来达到这种情况知识。



Edit

编辑

Just to clarify: Yes, an XHTML element can have multiple ids, e.g.

澄清一下:是的,一个 XHTML 元素可以有多个 id,例如

<p id="foo" xml:id="bar">

but assigning multiple ids to the same idattribute using a space-separated list is not possible.

但是id使用空格分隔的列表为同一属性分配多个 id是不可能的。

回答by Ross

My understanding has always been:

我的理解一直是:

  • ID's are single useand are only applied to one element...

    • Each is attributed as a Unique Identifier to (only) one single element.
  • Classes can be used more than once...

    • They can therefore be applied to more than one element, and similarly yet different, there can be more than one class (i.e. multiple classes) per element.
  • ID 是一次性使用的,仅适用于一个元素...

    • 每个都作为唯一标识符(仅)一个元素
  • 类可以多次使用...

    • 因此,它们可以应用于多个元素,并且类似但不同的是,每个元素可以有多个类(即多个类)

回答by acrosman

No.While the definition from w3cfor HTML 4 doesn't seem to explicitly cover your question, the definition of the name and id attributesays no spaces in the identifier:

不。虽然w3c的 HTML 4定义似乎没有明确涵盖您的问题,但 name 和 id 属性定义表示标识符中没有空格:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

ID 和 NAME 标记必须以字母 ([A-Za-z]) 开头,后面可以跟任意数量的字母、数字 ([0-9])、连字符 ("-")、下划线 ("_") 、冒号 (":") 和句点 (".")。

回答by tvanfosson

No. Every DOM element, if it has an id, has a single, unique id. You could approximate it using something like:

不是。每个 DOM 元素,如果它有一个 id,那么它就有一个唯一的 id。你可以使用类似的东西来近似它:

<div id='enclosing_id_123'><span id='enclosed_id_123'></span></div>

and then use navigation to get what you really want.

然后使用导航来获取您真正想要的东西。

If you are just looking to apply styles, class names are better.

如果您只是想应用样式,类名更好。

回答by AmbroseChapel

You can only have one ID per element, but you can indeed have more than one class. But don't have multiple class attributes, put multiple class values into one attribute.

每个元素只能有一个 ID,但确实可以有多个类。但是不要有多个类属性,将多个类值放在一个属性中。

<div id="foo" class="bar baz bax">

is perfectly legal.

是完全合法的。

回答by Anjisan

No, you should use nested DIVs if you want to head down that path. Besides, even if you could, imagine the confusion it would cause when you run document.getElementByID(). What ID is it going to grab if there are multiple ones?

不,如果你想沿着这条路走下去,你应该使用嵌套的 DIV。此外,即使你可以,想象一下当你运行 document.getElementByID() 时它会引起的混乱。如果有多个ID,它会抓取什么ID?

On a slightly related topic, you can add multiple classesto a DIV. See Eric Myers discussion at,

在一个稍微相关的主题上,您可以向 DIV添加多个。见埃里克迈尔斯的讨论,

http://meyerweb.com/eric/articles/webrev/199802a.html

http://meyerweb.com/eric/articles/webrev/199802a.html

回答by tpower

No you cannot have multiple ids for a single tag, but I have seen a tag with a nameattribute and an idattribute which are treated the same by some applications.

不,一个标签不能有多个 id,但我见过一个带有name属性和id属性的标签,它们被某些应用程序视为相同。

回答by Samdom For Peace

Any ID assigned to a div element is unique.
However, you can assign multiple IDs "under", and not "to" a div element.
In that case, you have to represent those IDs as <span></span> IDs.

Say, you want two links in the same HTML page to point to the same div element in  the page.

The two different links
<p><a href="#exponentialEquationsCalculator">Exponential Equations</a></p>         

<p><a href="#logarithmicExpressionsCalculator"><Logarithmic Expressions</a></p>

Point to the same section of the page. 
<!-- Exponential / Logarithmic Equations Calculator -->
<div class="w3-container w3-card white w3-margin-bottom">      
   <span id="exponentialEquationsCalculator"></span>
   <span id="logarithmicEquationsCalculator"></span>
</div>

回答by Alexandr

http://www.w3.org/TR/REC-html40/struct/global.html#h-7.5.2

http://www.w3.org/TR/REC-html40/struct/global.html#h-7.5.2

The id attribute assigns a uniqueidentifier to an element (which may be verified by an SGML parser).

id 属性为元素分配唯一标识符(可以由 SGML 解析器验证)。

and

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

ID 和 NAME 标记必须以字母 ([A-Za-z]) 开头,后面可以跟任意数量的字母、数字 ([0-9])、连字符 ("-")、下划线 ("_") 、冒号 (":") 和句点 (".")。

So "id" must be unique and can't contain a space.

所以“id”必须是唯一的,不能包含空格。