尝试一下
A normal, boring paragraph. Try not to fall asleep.
The most exciting paragraph on the page. One of a kind!
#exciting {
background: linear-gradient(to bottom, #ffe8d4, #f69d3c);
border: 1px solid #696969;
padding: 10px;
border-radius: 10px;
box-shadow: 2px 2px 1px black;
}
#exciting:before {
content: "ℹ️";
margin-right: 5px;
}
描述
它用于在链接(使用片段)、脚本和样式(通过 CSS)中辨识元素。
警告:该属性的值是一个不透明(opaque)字符串,这意味着网页开发者不能使用它来传递人类可读的信息。然而,具有一定可读性的 ID(如使用 ticket-18659 而不是 r45tgfe-freds&$@)可以有助于理解代码。
id 的值不得包含空格和制表符等空白字符。浏览器会将不符合规范的 ID 中的空白字符视为 ID 的一部分。与允许以空格分隔值的 class 属性不同,元素只能拥有一个 ID 值。
备注:从技术上讲,id 属性的值可以包含除了空白字符的任何字符。然而,为了避免无意中的错误,只能使用 ASCII 字母、数字、'_' 和 '-',并且id 属性的值应该以字母开头。例如,. 在 CSS 中具有特殊的意义(它作为一个类选择器)。除非你注意在 CSS 中转义它,否则它不会被识别为 id 属性值的一部分。很容易忘记这样做,导致你的代码中出现难以检测的错误。
规范
Specification
HTML# the-id-attribute
浏览器兼容性
参见
所有的全局属性。
反映该属性的 Element.id。
Document.getElementById 方法。
CSS ID 选择器。
Help improve MDN
Was this page helpful to you?
Yes
No
Learn how to contribute
This page was last modified on 2025年11月4日 by MDN contributors.
View this page on GitHub • Report a problem with this content