jQuery html() Metode

❮ jQuery HTML/CSS-metodes

Voorbeeld

Verander die inhoud van alle <p>-elemente:

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

Definisie en gebruik

Die html() metode stel of gee die inhoud (innerHTML) van die geselekteerde elemente terug.

Wanneer hierdie metode gebruik word om inhoud terug te gee , gee dit die inhoud van die EERSTE passende element terug.

Wanneer hierdie metode gebruik word om inhoud op te stel , oorskryf dit die inhoud van ALLE ooreenstemmende elemente.

Wenk: Om slegs die teksinhoud van die geselekteerde elemente te stel of terug te gee, gebruik die text() metode.


Sintaksis

Gee inhoud terug:

$(selector).html()

Stel inhoud:

$(selector).html(content)

Stel inhoud met behulp van 'n funksie:

$(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 dit self - voorbeelde


Hoe om die inhoud van 'n element terug te gee.


Gebruik 'n funksie om die inhoud van alle geselekteerde elemente te stel.


❮ jQuery HTML/CSS-metodes