id
全局属性定义了一个全文档唯一的标识符 (ID)。它用于在链接(使用片段)、脚本和样式(通过 CSS)中辨识元素。
该属性的值是一个不透明(opaque)字符串,这意味着网页开发者不能使用它来传递人类可读的信息。
id
的值不得包含空白字符(whitespace,包括空格和制表符等)。浏览器会将不符合规范的 ID 中的空白字符视为 ID 的一部分。与允许以空格分隔值的 class
属性不同,元素只能拥有一个 ID 值。
规范
规范 | 状态 | 备注 |
---|---|---|
HTML Living Standard id |
Living Standard | No change from latest snapshot, HTML 5.1 |
HTML 5.1 id |
Recommendation | Snapshot of HTML Living Standard, no change from HTML5 |
HTML5 id |
Recommendation | Snapshot of HTML Living Standard, now accept '_' , '-' and '.' if not at the beginning fo the id. It is also a true global attribute. |
HTML 4.01 Specification id |
Recommendation | Supported on all elements but <base> , <head> , <html> , <meta> , <script> , <style> , and <title> . |
浏览器兼容性
BCD tables only load in the browser
另请参阅
- 所有的全局属性。
- 反映该属性的
Element.id
。