Latest web development tutorials

jQuery text () method

jQuery HTML / CSS Methods jQuery HTML / CSS Methods

Examples

Set the text contents of all <p> element:

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

try it"

Definition and Usage

text () method sets or returns the text content of the selected element.

When this method returns, returns all matching elements of the text (HTML tags will be deleted).

When this method is used to set the content, rewrite all matching elements content.

Tip: To set or return the selected element innerHTML (text + HTML tags), use html () method.


grammar

Back text:

$(selector).text()

Setting text:

$(selector).text( content )

Use the function to set the text:

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

参数 描述
content 必需。规定被选元素的新文本内容。

注意: 特殊字符会被编码。
function(index,currentcontent) 可选。规定返回被选元素的新文本内容的函数。
  • index- 返回集合中元素的 index 位置。
  • currentcontent- 返回被选元素的当前内容。


Examples

More examples

Back text
How to return the text content of the selected element.

Use the function to set the text content
Use the function to set the text contents of the selected element.


jQuery HTML / CSS Methods jQuery HTML / CSS Methods