html元素可以有多个ID吗?

本文翻译自: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兼容性指南。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 是的,一个 `<div>` 标签可以拥有多个 `class`,可以用空格分隔。例如: ```html <div class="class1 class2 class3"></div> ``` 如果你想修改一个 `<div>` 标签的类名,可以通过 JavaScript 来实现。例如: ```javascript var div = document.querySelector('div'); // 获取第一个 <div> 标签 div.classList.remove('class1'); // 移除原来的 class1 类名 div.classList.add('newClass'); // 添加新的 newClass 类名 ``` 如果你想同时修改多个 `<div>` 标签的类名,可以使用 `querySelectorAll` 方法来获取多个元素,然后使用 `forEach` 方法对每个元素进行操作。例如: ```javascript var divs = document.querySelectorAll('div'); // 获取所有 <div> 标签 divs.forEach(function(div) { div.classList.remove('class1'); // 移除原来的 class1 类名 div.classList.add('newClass'); // 添加新的 newClass 类名 }); ``` ### 回答2: 在HTML中,一个div元素可以同时拥有多个class。我们可以通过在class属性中添加多个class名称,以空格分隔它们。例如: ```html <div class="class1 class2 class3"> 这是一个拥有多个class的div </div> ``` 上述代码中,div元素拥有三个class,分别是"class1"、"class2"和"class3"。这样做的好处是可以同时应用不同的样式或功能于同一个元素,使其具备多种特性。 当需要修改一个div元素的class时,我们可以使用JavaScript或者JavaScript的库或框架来实现。下面我将以纯JavaScript实现为例: ```html <div id="myDiv" class="oldClass"> 这是一个div </div> <script> var divElement = document.getElementById("myDiv"); divElement.classList.remove("oldClass"); // 移除原有class divElement.classList.add("newClass"); // 添加新的class </script> ``` 上述代码中,我们首先通过getElementById()方法获取到id为"myDiv"的div元素,然后使用classList属性来进行对class的操作。通过remove()方法移除原有的class,再通过add()方法添加新的class。 这样,我们就可以动态地修改一个div元素的class,并实现所需的样式或功能。 ### 回答3: 是的,一个HTML元素可以具有多个class。在HTML中,可以使用空格将多个class名称分隔开。例如: <div class="class1 class2 class3">这是一个div元素</div> 在上述示例中,该div元素具有三个不同的class名称:class1、class2和class3。通过为元素添加多个class,可以同时应用多种样式和功能。这允许开发者更灵活地定制和设计HTML元素。在CSS中,可以通过选择器来选择并样式化具有特定class名称的元素。同时,也可以使用JavaScript来通过class名称对元素进行操作和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值