Home >Web Front-end >JS Tutorial >jQuery's text() method usage analysis_jquery
The example in this article describes the usage of jQuery’s text() method. Share it with everyone for your reference. The specific analysis is as follows:
This method returns or sets the text content of the matching element.
For more information, please refer to the reference manual text() method.
Special instructions:
HTML content means that the content can contain HTML tags and can be rendered by the browser.
The text content is to first convert the HTML predefined characters in the content into HTML character entities, so that the HTML tags will not be rendered.
Usage of text() method:
Usage 1:
When this method takes no parameters, its function is to obtain the text content of all matching elements, and the result is the text combined by the text content contained in all matching elements.
This method is similar to the html() method without parameters, but there are still big differences:
1.html() method obtains the content of the first matching element, while text() method obtains the content contained in all matching elements.
2. The html() method obtains the html content, while the text() method obtains the text content.
The example code is as follows:
The above code can pop up and obtain all the text content in the div.
Usage 2:
With parameters, it sets the text content of all matching elements.
This method is similar to the html() method with parameters, but there are still big differences:
The text() method sets the text content of the matching element, and converts reserved characters in HTML (such as the greater than sign (>)) into html character entities for correct display, while the html() method sets the matching The html content of the element.
Example code:
Example code three:
通过此实例大家可以观察一下HTML内容和文本内容的区别。
希望本文所述对大家的jQuery程序设计有所帮助。