Attributo ID HTML
Esempio
Usa l'attributo id per manipolare il testo con JavaScript:
<html>
<body>
<h1 id="myHeader">Hello World!</h1>
<button onclick="displayResult()">Change text</button>
<script>
function displayResult()
{
document.getElementById("myHeader").innerHTML = "Have a nice day!";
}
</script>
</body>
</html>
Altri esempi "Provalo da solo" di seguito.
Definizione e utilizzo
L' id
attributo specifica un ID univoco per un elemento HTML (il valore deve essere univoco all'interno del documento HTML).
L' id
attributo è più utilizzato per puntare a uno stile in un foglio di stile e da JavaScript (tramite il DOM HTML) per manipolare l'elemento con l'id specifico.
Supporto browser
Attribute | |||||
---|---|---|---|---|---|
id | Yes | Yes | Yes | Yes | Yes |
Sintassi
<element id="id">
Valori di attributo
Value | Description |
---|---|
id | Specifies a unique id for the element.
Naming rules:
|
Altri esempi
Esempio 1
Utilizza l'attributo id per collegarti a un elemento con un ID specificato all'interno di una pagina:
<html>
<body>
<h2><a id="top">Some heading</a></h2>
<p>Lots of text....</p>
<p>Lots of text....</p>
<p>Lots of text....</p>
<a href="#top">Go to top</a>
</body>
</html>
Esempio 2
Usa l'attributo id per dare uno stile al testo con CSS:
<html>
<head>
<style>
#myHeader {
color: red;
text-align: center;
}
</style>
</head>
<body>
<h1 id="myHeader">W3Schools is the best!</h1>
</body>
</html>
Pagine correlate
Esercitazione HTML: ID HTML
Esercitazione HTML: attributi HTML
Esercitazione CSS: sintassi CSS
Riferimento CSS: Selettore CSS #id
Riferimento HTML DOM: Metodo HTML DOM getElementById()
Riferimento HTML DOM: Proprietà ID HTML DOM
Riferimento HTML DOM: Oggetto stile HTML DOM