jQuery html() Methode

❮ jQuery HTML/CSS-methoden

Voorbeeld

Wijzig de inhoud van alle <p> elementen:

$("button").click(function(){
  $("p").html("Hello <b>world</b>!");
});

Definitie en gebruik

De methode html() stelt de inhoud (innerHTML) van de geselecteerde elementen in of geeft deze terug.

Wanneer deze methode wordt gebruikt om inhoud te retourneren , wordt de inhoud van het EERSTE overeenkomende element geretourneerd.

Wanneer deze methode wordt gebruikt om inhoud in te stellen , wordt de inhoud van ALLE overeenkomende elementen overschreven.

Tip: Gebruik de methode text() om alleen de tekstinhoud van de geselecteerde elementen in te stellen of te retourneren .


Syntaxis

Inhoud retourneren:

$(selector).html()

Inhoud instellen:

$(selector).html(content)

Stel inhoud in met een functie:

$(selector).html(function(index,currentcontent))

Parameter Description
content Required. Specifies the new content for the selected elements (can contain HTML tags)
function(index,currentcontent) Optional. Specifies a function that returns the new content for the selected elements
  • index - Returns the index position of the element in the set
  • currentcontent - Returns the current HTML content of the selected element

Probeer het zelf - Voorbeelden


Hoe de inhoud van een element te retourneren.


Een functie gebruiken om de inhoud van alle geselecteerde elementen in te stellen.


❮ jQuery HTML/CSS-methoden