Element:id 属性

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Element 接口的 id 属性表示元素的标识符,反映了全局属性 id 的值。

如果 id 的值不为空字符串,则在同一文档中必须唯一。

这有助于让常用的 getElementById() 方法通过 id 的值找到对应的单个元素。另外,在使用 CSS 为文档添加样式时,ID 可作为选择器使用。

备注: 虽然 ID 是区分大小写的,但是不应该同时使用仅大小写不同的 ID 值。

一个字符串。

规范

Specification
DOM
# ref-for-dom-element-id①

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
id

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
Has more compatibility info.

参见

  • DOM 全局属性 id