Metoda jQuery text() .

❮ jQuery HTML/CSS metody

Příklad

Nastavit textový obsah pro všechny prvky <p>:

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

Definice a použití

Metoda text() nastavuje nebo vrací textový obsah vybraných prvků.

Když se tato metoda použije k vrácení obsahu, vrátí textový obsah všech odpovídajících prvků (značka HTML bude odstraněna).

Když je tato metoda použita k nastavení obsahu, přepíše obsah VŠECH odpovídajících prvků.

Tip: Chcete-li nastavit nebo vrátit vnitřní HTML (text + označení HTML) vybraných prvků, použijte metodu html() .


Syntax

Vrátit textový obsah:

$(selector).text()

Nastavit obsah textu:

$(selector).text(content)

Nastavte textový obsah pomocí funkce:

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

Zkuste si to sami - příklady


Jak vrátit textový obsah vybraných prvků.


Pomocí funkce nastavte obsah textu vybraných prvků.


❮ jQuery HTML/CSS metody