Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery text() method

jQuery text() method

jQuery HTML/CSS methods jQuery HTML/CSS methods

Example

Set all

The text content of the element:

$

(

"

button

"

)

.

click

(

function

(

)

{

$

(

"

p

"

)

.

text

(

"

Hello world!

"

)

;

}

)

;

Try it »


Definition and Usage

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

When this method is used toReturncontent, it returns the text content of all matched elements (HTML tags are removed).

When this method is used toSettingscontent, it overwrites the content of all matched elements.

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


Syntax

Returning text content:

$(

selector

).text()

Setting text content:

$(

selector

).text(

content

)

Setting text content using a function:

$(

selector

).text(function

(index,currentcontent)

)

Parameters Description
content Required. Specifies the new text content of the selected elements.
 **注意:** 特殊字符会被编码。 |

| function(index,currentcontent) | Optional. Specifies a function that returns the new text content of the selected elements.

|

More examples

Returning text content
How to return the text content of the selected elements.

Setting text content using a function
Use a function to set the text content of the selected elements.


jQuery HTML/CSS methods jQuery HTML/CSS methods