JavaScript DOM-elementer


Indholdsfortegnelse

    Vis indholdsfortegnelse


Denne side lærer dig, hvordan du finder og får adgang til HTML-elementer i en HTML-side.


Find HTML-elementer

Ofte vil du med JavaScript manipulere HTML-elementer.

For at gøre det skal du først finde elementerne. Der er flere måder at gøre dette på:

  • Find HTML-elementer efter id

  • Find HTML-elementer efter tagnavn

  • Find HTML-elementer efter klassenavn

  • Find HTML-elementer ved hjælp af CSS-vælgere

  • Find HTML-elementer efter HTML-objektsamlinger


Find HTML-element efter id

Den nemmeste måde at finde et HTML-element i DOM er ved at bruge element-id'et.

Dette eksempel finder elementet med id="intro":

Eksempel

const element = document.getElementById("intro");

Prøv det selv →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript HTML DOM</h2>

<p id="intro">Finding HTML Elements by Id</p>
<p>This example demonstrates the <b>getElementsById</b> method.</p>

<p id="demo"></p>

<script>
const element = document.getElementById("intro");

document.getElementById("demo").innerHTML = 
"The text from the intro paragraph is: " + element.innerHTML;

</script>

</body>
</html>

Hvis elementet findes, vil metoden returnere elementet som et objekt (i element).

Hvis elementet ikke findes, vil elementet indeholde null.


Find HTML-elementer efter tagnavn

Dette eksempel finder alle ><p>-elementer:

Eksempel

const element = document.getElementsByTagName("p");

Prøv det selv →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript HTML DOM</h2>

<p>Finding HTML Elements by Tag Name.</p>
<p>This example demonstrates the <b>getElementsByTagName</b> method.</p>

<p id="demo"></p>

<script>
const element = document.getElementsByTagName("p");

document.getElementById("demo").innerHTML = 'The text in first paragraph (index 0) is: ' + element[0].innerHTML;

</script>

</body>
</html>

Dette eksempel finder elementet med id="main" og finder derefter alle <p>-elementer inde i "main":

Eksempel

const x = document.getElementById("main");
const y = x.getElementsByTagName("p");

Prøv det selv →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript HTML DOM</h2>

<div id="main">
<p>Finding HTML Elements by Tag Name</p>
<p>This example demonstrates the <b>getElementsByTagName</b> method.</p>
</div>

<p id="demo"></p>

<script>
const x = document.getElementById("main");
const y = x.getElementsByTagName("p");

document.getElementById("demo").innerHTML = 
'The first paragraph (index 0) inside "main" is: ' + y[0].innerHTML;

</script>

</body>
</html>


Find HTML-elementer efter klassenavn

Hvis du vil finde alle HTML-elementer med det samme klassenavn, skal du bruge getElementsByClassName().

Dette eksempel returnerer en liste over alle elementer med class="intro".

Eksempel

const x = document.getElementsByClassName("intro");

Prøv det selv →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript HTML DOM</h2>

<p>Finding HTML Elements by Class Name.</p>
<p class="intro">Hello World!</p>
<p class="intro">This example demonstrates the <b>getElementsByClassName</b> method.</p>

<p id="demo"></p>

<script>
const x = document.getElementsByClassName("intro");
document.getElementById("demo").innerHTML = 
'The first paragraph (index 0) with class="intro" is: ' + x[0].innerHTML;
</script>

</body>
</html>

Find HTML-elementer af CSS-vælgere

Hvis du vil finde alle HTML-elementer, der matcher en specificeret CSS-vælger (id, klassenavne, typer, attributter, værdier af attributter osv.), brug metoden querySelectorAll().

Dette eksempel returnerer en liste over alle <p>-elementer med class="intro".

Eksempel

const x = document.querySelectorAll("p.intro");

Prøv det selv →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript HTML DOM</h2>

<p>Finding HTML Elements by Query Selector</p>
<p class="intro">Hello World!.</p>
<p class="intro">This example demonstrates the <b>querySelectorAll</b> method.</p>

<p id="demo"></p>

<script>
const x = document.querySelectorAll("p.intro");
document.getElementById("demo").innerHTML = 
'The first paragraph (index 0) with class="intro" is: ' + x[0].innerHTML;
</script>

</body>
</html>

Find HTML-elementer efter HTML-objektsamlinger

Dette eksempel finder formelementet med id="frm1" i formularerne samling og viser alle elementer værdier:

Eksempel

const x = document.forms["frm1"];
let text = "";
for (let i = 0; i < x.length; i++) {
  text += x.elements[i].value + "<br>";
}
document.getElementById("demo").innerHTML = text;

Prøv det selv →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript HTML DOM</h2>
<p>Finding HTML Elements Using <b>document.forms</b>.</p>

<form id="frm1" action="/action_page.php">
  First name: <input type="text" name="fname" value="Donald"><br>
  Last name: <input type="text" name="lname" value="Duck"><br><br>
  <input type="submit" value="Submit">
</form> 

<p>These are the values of each element in the form:</p>

<p id="demo"></p>

<script>
const x = document.forms["frm1"];
let text = "";
for (let i = 0; i < x.length ;i++) {
  text += x.elements[i].value + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Følgende HTML-objekter (og objektsamlinger) er også tilgængelige:

    dokument.ankre

    <!DOCTYPE html>
    <html>
    <body>
    
    <h2>Finding HTML Elements Using document.anchors</h2>
    
    <a name="html">HTML Tutorial</a><br>
    <a name="css">CSS Tutorial</a><br>
    <a name="xml">XML Tutorial</a><br>
    
    <p id="demo"></p>
    
    <script>
    document.getElementById("demo").innerHTML =
    "Number of anchors are: " + document.anchors.length;
    </script>
    
    
    </body>
    </html>
    

    document.body

    <!DOCTYPE html>
    <html>
    <body>
    
    <h2>JavaScript HTMLDOM</h2>
    <p>Displaying document.body</p>
    
    <p id="demo"></p>
    
    <script>
    document.getElementById("demo").innerHTML = document.body.innerHTML;
    </script>
    
    </body>
    </html>
    

    document.documentElement

    <!DOCTYPE html>
    <html>
    <body>
    
    <h2>JavaScript HTMLDOM</h2>
    <p>Displaying document.documentElement</p>
    
    <p id="demo"></p>
    
    <script>
    document.getElementById("demo").innerHTML = document.documentElement.innerHTML;
    </script>
    
    </body>
    </html>
    

    dokument.indlejrer

    <!DOCTYPE html>
    <html>
    <body>
    
    <h2>JavaScript HTMLDOM</h2>
    <p id="demo"></p>
    
    <script>
    document.getElementById("demo").innerHTML =
    "Number of embeds: " + document.embeds.length;
    </script>
    
    </body>
    </html>
    

    dokument.formularer

    <!DOCTYPE html>
    <html>
    <body>
    
    <h2>Finding HTML Elements Using document.forms</h2>
    
    <form action="">
    First name: <input type="text" name="fname" value="Donald">
    <input type="submit" value="Submit">
    </form> 
    
    <p id="demo"></p>
    
    <script>
    document.getElementById("demo").innerHTML =
    "Number of forms: " + document.forms.length;
    </script>
    
    </body>
    </html>
    

    dokument.hoved

    <!DOCTYPE html>
    <html>
    
    <head>
    <title>W3Schools Demo</title>
    </head>
    
    <body>
    
    <h2>JavaScript HTMLDOM</h2>
    <p id="demo"></p>
    
    <script>
    document.getElementById("demo").innerHTML =
    document.head;
    </script>
    
    </body>
    </html>
    

    dokument.billeder

    <!DOCTYPE html>
    <html>
    <body>
    
    <h2>Finding HTML Elements Using document.images</h2>
    
    <img src="pic_htmltree.gif" width="486" height="266">
    <img src="pic_navigate.gif" width="362" height="255">
    
    <p id="demo"></p>
    
    <script>
    document.getElementById("demo").innerHTML =
    "Number of images: " + document.images.length;
    </script>
    
    </body>
    </html>
    

    document.links

    <!DOCTYPE html>
    <html>
    <body>
    
    <h2>Finding HTML Elements Using document.links</h2>
    
    <p>
    <a href="/html/default.asp">HTML</a>
    <br>
    <a href="/css/default.asp">CSS</a>
    </p>
    
    <p id="demo"></p>
    
    <script>
    document.getElementById("demo").innerHTML =
    "Number of links: " + document.links.length;
    </script>
    
    </body>
    </html>
    

    document.scripts

    <!DOCTYPE html>
    <html>
    <body>
    
    <h2>Finding HTML Elements Using document.scripts</h2>
    
    <p id="demo"></p>
    
    <script>
    document.getElementById("demo").innerHTML =
    "Number of scripts: " + document.scripts.length;
    </script>
    
    </body>
    </html>
    

    document.title

    <!DOCTYPE html>
    <html>
      <head>
      <title>W3Schools Demo</title>
      </head>
    <body>
    
    <h2>Finding HTML Elements Using document.title</h2>
    
    <p id="demo"></p>
    
    <script>
    document.getElementById("demo").innerHTML =
    "The title of this document is: " + document.title;
    </script>
    
    </body>
    </html>