HTML id kenmerk


Voorbeeld

Gebruik die id-kenmerk om teks met JavaScript te manipuleer:

<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>

Meer "Probeer dit self" voorbeelde hieronder.


Definisie en gebruik

Die idkenmerk spesifiseer 'n unieke ID vir 'n HTML-element (die waarde moet uniek wees binne die HTML-dokument).

Die idkenmerk word die meeste gebruik om na 'n styl in 'n stylblad te wys, en deur JavaScript (via die HTML DOM) om die element met die spesifieke id te manipuleer.


Blaaierondersteuning

Attribute
id Yes Yes Yes Yes Yes

Sintaksis

<element id="id">

Eienskapwaardes

Value Description
id Specifies a unique id for the element. Naming rules:
  • Must contain at least one character
  • Must not contain any space characters

Meer voorbeelde

Voorbeeld 1

Gebruik die id-kenmerk om te skakel na 'n element met 'n gespesifiseerde id binne 'n bladsy:

<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>

Voorbeeld 2

Gebruik die id-kenmerk om teks met CSS te styl:

<html>
<head>
<style>
#myHeader {
  color: red;
  text-align: center;
}
</style>
</head>
<body>

<h1 id="myHeader">W3Schools is the best!</h1>

</body>
</html>

Verwante bladsye

HTML-tutoriaal: HTML-ID

HTML-tutoriaal: HTML-kenmerke

CSS-tutoriaal: CSS-sintaksis

CSS-verwysing: CSS #id Selector

HTML DOM Verwysing: HTML DOM getElementById() Metode

HTML DOM Verwysing: HTML DOM id Eiendom

HTML DOM Verwysing: HTML DOM Style Object