Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between text and html in jquery

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what is the difference between text and html in jquery". In daily operation, I believe that many people have doubts about the difference between text and html in jquery. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "what is the difference between text and html in jquery?" Next, please follow the editor to study!

Differences: 1, when the html () method gets and sets the element, it sets the html code, and when the text () method gets and sets the element, it sets the element text content; 2, the html () method does not support the use of xml, while the text () method supports html and xml.

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

What is the difference between text and html in jquery? 1. Html () method

This method is similar to the innerHTML attribute in JavaScript and can be used to read or set the HTML content in an element.

In order to show the effect more clearly, set the

The HTML code of the element is changed to:

Who is your favorite boy?

Then use the html () method to

Element to obtain:

Var p_html = $("p") .html (); / / get

The HTML code alert (p_html) in the element; / / print

HTML code of the element

After running the code, the effect is as follows:

Get the HTML code of the entire p element

If you need to set the HTML code for an element, you can also use this method, but you need to pass a parameter to it. For example, to set the

The HTML code for the element, you can use the following code

$("p"). Html ("who is your favorite boy?") ; / / Settings

HTML code of the element

Note: the html () method can be used for XHTML documents, but not for XML documents.

2. Text () method

This method is similar to the innerText attribute in JavaScript and can be used to read and set the text content in an element.

Continue to use the above HTML code:

Who is your favorite boy?

Using the text () method to

Element to obtain:

Var p_text = $("p") .text (); / / get

Alert (p_text) of the text content of the element; / / print

The text content of the element

After running the code, the effect is as follows:

Show only text content, do not include and display html code

As with the html () method, if you need to set the text content for an element, you also need to pass a parameter. For example, to

Element to set the text content, as follows:

$("p"). Text ("who is your favorite boy?") ; / / Settings

The text content of the element

Note: (1) the innerText property in JavaScript does not work under some versions of Firefox browsers, while the text () method of jQuery supports all browsers.

(2) the text () method is valid for both HTML and XML documents.

At this point, the study on "what is the difference between text and html in jquery" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report