ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# HTML id 属性 [HTML 全局属性](/tags/html_ref_standardattributes.asp) ## 实例 通过 JavaScript 利用 id 属性来改变一段文本: ``` <html> <head> <script type="text/javascript"> function change_header() { document.getElementById("myHeader").innerHTML="Nice day!"; } </script> </head> <body> <h1 id="myHeader">Hello World!</h1> <button onclick="change_header()">Change text</button> </body> </html> ``` (可以在本页底部找到更多实例) ## 定义和用法 id 属性规定 HTML 元素的唯一的 id。 id 在 HTML 文档中必须是唯一的。 id 属性可用作链接锚(link anchor),通过 JavaScript(HTML DOM)或通过 CSS 为带有指定 id 的元素改变或添加样式。 ## 支持 W3C: "W3C" 列指示 W3C 的 HTML/XHTML 推荐标准中是否定义了该属性。 | IE | Firefox | Opera | Safari | W3C | | --- | --- | --- | --- | --- | | YES | YES | YES | YES | YES | ## 语法 ``` <element id="_value_"> ``` ### 属性值 | 值 | 描述 | | --- | --- | | _id_ | 规定元素的唯一 id。 | ## 亲自试一试 - 实例 [向 HTML 元素添加唯一的 id](/tiy/t.asp?f=html_standard_id) 如何向 HTML 元素添加 id 属性。 [在 CSS 中使用 id 属性](/tiy/t.asp?f=html_standard_id_css) 如何在 CSS 中使用 id 属性 [HTML 全局属性](/tags/html_ref_standardattributes.asp)