id
global attribute
defines an identifier (ID) which must be unique in the whole document. Its purpose is to identify the element when linking (using a
fragment identifier
), scripting, or styling (with
CSS
).
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
This attribute's value is an opaque string: this means that web authors should not rely on it to convey human-readable information (although having your IDs somewhat human-readable can be useful for code comprehension, e.g. consider
ticket-18659
versus
r45tgfe-freds&$@
).
id
's value must not contain
whitespace
(spaces, tabs etc.). Browsers treat non-conforming IDs that contain whitespace as if the whitespace is part of the ID. In contrast to the
class
attribute, which allows space-separated values, elements can only have one single ID value.
注意:
Using characters except
ASCII
letters, digits,
'_'
,
'-'
and
'.'
may cause compatibility problems, as they weren't allowed in HTML 4. Though this restriction has been lifted in
HTML5
, an ID should start with a letter for compatibility.
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'id' in that specification. |
实时标准 | No change from latest snapshot, HTML 5.1 |
|
HTML 5.1
The definition of 'id' in that specification. |
推荐 | 快照 HTML 实时标准 , no change from HTML5 |
|
HTML5
The definition of 'id' in that specification. |
推荐 |
快照
HTML 实时标准
, now accept
'_'
,
'-'
and
'.'
if not at the beginning of the id. It is also a true global attribute.
|
|
HTML 4.01 Specification
The definition of 'id' in that specification. |
推荐 |
Supported on all elements but
<base>
,
<head>
,
<html>
,
<meta>
,
<script>
,
<style>
,和
<title>
.
|
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | Android webview | Chrome for Android | Firefox for Android | Opera for Android | Safari on iOS | Samsung Internet | |
id
|
Chrome 完整支持 Yes | Edge 完整支持 12 |
Firefox
完整支持
32
|
IE 完整支持 Yes | Opera 完整支持 Yes | Safari 完整支持 Yes | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes |
Firefox Android
完整支持
32
|
Opera Android 完整支持 Yes | Safari iOS 完整支持 Yes | Samsung Internet Android 完整支持 Yes |
完整支持
见实现注意事项。
Element.id
that reflects this attribute.
<a>
<abbr>
<acronym>
<address>
<applet>
<area>
<article>
<aside>
<audio>
<b>
<base>
<basefont>
<bdi>
<bdo>
<bgsound>
<big>
<blink>
<blockquote>
<body>
<br>
<button>
<canvas>
<caption>
<center>
<cite>
<code>
<col>
<colgroup>
<command>
<content>
<data>
<datalist>
<dd>
<del>
<details>
<dfn>
<dialog>
<dir>
<div>
<dl>
<dt>
<element>
<em>
<embed>
<fieldset>
<figcaption>
<figure>
<font>
<footer>
<form>
<frame>
<frameset>
<h1>
<head>
<header>
<hgroup>
<hr>
<html>
<i>
<iframe>
<image>
<img>
<input>
<ins>
<isindex>
<kbd>
<keygen>
<label>
<legend>
<li>
<link>
<listing>
<main>
<map>
<mark>
<marquee>
<menu>
<menuitem>
<meta>
<meter>
<multicol>
<nav>
<nextid>
<nobr>
<noembed>
<noframes>
<noscript>
<object>
<ol>
<optgroup>
<option>
<output>
<p>
<param>
<picture>
<plaintext>
<pre>
<progress>
<q>
<rb>
<rp>
<rt>
<rtc>
<ruby>
<s>
<samp>
<script>
<section>
<select>
<shadow>
<slot>
<small>
<source>
<spacer>
<span>
<strike>
<strong>
<style>
<sub>
<summary>
<sup>
<table>
<tbody>
<td>
<template>
<textarea>
<tfoot>
<th>
<thead>
<time>
<title>
<tr>
<track>
<tt>
<u>
<ul>
<var>
<video>
<wbr>
<xmp>
<input>
类型
<input type="button">
<input type="checkbox">
<input type="color">
<input type="date">
<input type="datetime">
<input type="datetime-local">
<input type="email">
<input type="file">
<input type="hidden">
<input type="image">
<input type="month">
<input type="number">
<input type="password">
<input type="radio">
<input type="range">
<input type="reset">
<input type="search">
<input type="submit">
<input type="tel">
<input type="text">
<input type="time">
<input type="url">
<input type="week">