Metoda jQuery html()

❮ Metody jQuery HTML/CSS

Przykład

Zmień zawartość wszystkich <p> elementów:

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

Definicja i użycie

Metoda html() ustawia lub zwraca zawartość (innerHTML) wybranych elementów.

Gdy ta metoda jest używana do zwracania zawartości, zwraca ona zawartość PIERWSZEGO dopasowanego elementu.

Gdy ta metoda jest używana do ustawiania zawartości, zastępuje zawartość WSZYSTKICH dopasowanych elementów.

Wskazówka: Aby ustawić lub zwrócić tylko treść tekstową wybranych elementów, użyj metody text() .


Składnia

Treść zwrotu:

$(selector).html()

Ustaw zawartość:

$(selector).html(content)

Ustaw zawartość za pomocą funkcji:

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

Spróbuj sam - przykłady


Jak zwrócić zawartość elementu.


Za pomocą funkcji ustaw zawartość wszystkich wybranych elementów.


❮ Metody jQuery HTML/CSS