jQuery text() Methode

❮ jQuery HTML/CSS-methoden

Voorbeeld

Stel tekstinhoud in voor alle <p> elementen:

$("button").click(function(){
  $("p").text("Hello world!");
});

Definitie en gebruik

De methode text() stelt de tekstinhoud van de geselecteerde elementen in of geeft deze terug.

Wanneer deze methode wordt gebruikt om inhoud te retourneren , wordt de tekstinhoud van alle overeenkomende elementen geretourneerd (HTML-opmaak wordt verwijderd).

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

Tip: Om de innerHTML (tekst + HTML-opmaak) van de geselecteerde elementen in te stellen of te retourneren, gebruikt u de html()- methode.


Syntaxis

Tekstinhoud retourneren:

$(selector).text()

Tekstinhoud instellen:

$(selector).text(content)

Stel tekstinhoud in met een functie:

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

Parameter Description
content Required. Specifies the new text content for the selected elements

Note: Special characters will be encoded
function(index,currentcontent) Optional. Specifies a function that returns the new text content for the selected elements
  • index - Returns the index position of the element in the set
  • currentcontent - Returns current content of selected elements

Probeer het zelf - Voorbeelden


Hoe tekstinhoud van de geselecteerde elementen te retourneren.


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


❮ jQuery HTML/CSS-methoden