id

id 全局属性定义了一个全文档唯一的标识符 (ID)。它用于在链接(使用片段)、脚本和样式(通过 CSS)中辨识元素。

该属性的值是一个透明(opaque)字符串,这意味着网页开发者不能使用它来传递人类可读的信息。

id 的值不得包含空白字符(whitespace,包括空格和制表符等)。浏览器会将不符合规范的 ID 中的空白字符视为 ID 的一部分。与允许以空格分隔值的 class 属性不同,元素只能拥有一个 ID 值。

注意:使用除 ASCII 字母、数字、_-. 以外的字符可能会造成兼容性问题,因为 HTML 4 中不允许使用它们。虽然这个限制在 HTML5 中被解除了,但为兼容性考虑 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>.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
idChrome Full support YesEdge Full support YesFirefox Full support 32
Full support 32
No support ? — 32
Notes
Notes id is a true global attribute only since Firefox 32.
IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 32
Full support 32
No support ? — 32
Notes
Notes id is a true global attribute only since Firefox 32.
Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support
See implementation notes.
See implementation notes.

另请参阅