Home  >  Article  >  Web Front-end  >  Specifies the unique id attribute of the html element

Specifies the unique id attribute of the html element

黄舟
黄舟Original
2017-11-03 11:49:303527browse

Example

Use the id attribute to change a piece of text through JavaScript:

<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>
亲自试一试

Definition and usage

id attribute Specifies the unique id of the HTML element.

id must be unique within the HTML document.

The id attribute can be used as a link anchor to change or add styles to an element with a specified id via JavaScript (HTML DOM) or via CSS.

Support

W3C: The "W3C" column indicates whether the attribute is defined in the W3C's HTML/XHTML recommendations.

IE Firefox Opera Safari W3C
YES YES YES YES YES

Syntax

<element id="value">
Value Description
id Specifies the unique id of the element.

There may be several reasons for using the id attribute

  • You want to link to a specified location on a page

  • You want to use a specific css style on a certain HTML element

  • You want to use script for each HTML element (such as javascript)

  • You want to differentiate between two identical HTML elements

Example

<div id="dreamdu1">使用dreamdu1和dreamdu2来区分两个div标签<div><div id="dreamdu2">使用dreamdu1和dreamdu2来区分两个div标签<div>

Instructions

  • The same attribute as the id attribute is name. This attribute can be used in HTML4 documents. The new web standard has abandoned this attribute. Therefore, when using the name attribute, you should use the XHTML transition definition type (xhtml1- transitional.dtd), cannot be used in XHTML strictly defined types (xhtml1-strict.dtd)

  • The id attribute must be unique in the entire page and CSS style sheet, and in the entire website The best and only

The above is the detailed content of Specifies the unique id attribute of the html element. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn