本文翻译自:Can an html element have multiple ids?
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感到好奇。
#1楼
参考:https://stackoom.com/question/nxY/html元素可以有多个ID吗
#2楼
I'd like to say technically yes, since really what gets rendered is technically always browser dependent. 我想从技术上说是的,因为从技术上讲,渲染的内容实际上始终取决于浏览器。 Most browsers try to keep to the specifications as best they can and as far as I know there is nothing in the css specifications against it. 据我所知,大多数浏览器都尽可能地遵守规范,据我所知,css规范中没有针对它的内容。 I'm only going to vouch for the actual html,css,javascript code that gets sent to the browser before any other interpretter steps in. 我只会保证在任何其他解释程序介入之前发送到浏览器的实际html,css,javascript代码。
However I also say no since every browser I typically test on doesn't actually let you. 但是我也说不,因为我通常测试的每种浏览器实际上都不允许您使用。 If you need to see for yourself save the following as a .html file and open it up in the major browsers. 如果您需要亲自查看,请将以下内容另存为.html文件,然后在主要浏览器中将其打开。 In all browsers I tested on the javascript function will not match to an element. 我在javascript函数上测试过的所有浏览器都不会与元素匹配。 However, remove either "hunkojunk" from the id tag and all works fine. 但是,从id标记中删除任何一个“ hunkojunk”,然后一切正常。 Sample Code 样例代码
<html>
<head>
</head>
<body>
<p id="hunkojunk1 hunkojunk2"></p>
<script type="text/javascript">
document.getElementById('hunkojunk2').innerHTML = "JUNK JUNK JUNK JUNK JUNK JUNK";
</script>
</body>
</html>
#3楼
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 unique identifier 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”必须唯一且不能包含空格。
#4楼
That's interesting, but as far as I know the answer is a firm no. 这很有趣,但据我所知答案是肯定的。 I don't see why you need a nested ID, since you'll usually cross it with another element that has the same nested ID. 我不知道为什么需要嵌套ID,因为您通常将其与具有相同嵌套ID的另一个元素交叉。 If you don't there's no point, if you do there's still very little point. 如果不这样做,那就没有意义了。
#5楼
不,您不能为单个标签拥有多个ID,但是我看到一个带有name
属性和id
属性的标签,某些应用程序将它们视为相同。
#6楼
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. 在XML中,片段标识符的类型为ID,每个元素只能有一个类型为ID的属性。 Therefore, in XHTML 1.0 the id attribute is defined to be of type ID. 因此,在XHTML 1.0中,将id属性定义为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. 为了确保XHTML 1.0文档是结构良好的XML文档,在上面列出的元素上定义片段标识符时,XHTML 1.0文档必须使用id属性。 See the HTML Compatibility Guidelines for information on ensuring such anchors are backward compatible when serving XHTML documents as media type text/html. 有关确保在将XHTML文档作为媒体类型text / html提供服务时确保此类锚向后兼容的信息,请参见HTML兼容性指南。