一个 HTML 元素可以有多个 id 吗? [英] Can an HTML element have multiple ids?

查看:72
本文介绍了一个 HTML 元素可以有多个 id 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

解决方案

没有.来自 XHTML 1.0 规范

<块引用>

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

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

For a given element, can I assign multiple ids to it?

<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.

解决方案

No. From the XHTML 1.0 Spec

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.

这篇关于一个 HTML 元素可以有多个 id 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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