Js uses the html () and text () methods to obtain and set the display value of the p tag _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to obtain and set the p tag display value through the html () and text () methods in js. For more information, see 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. To obtain the content of an element, you can:

The Code is as follows:


Var p_html = $ ("p" ).html (); // obtain the HTML code of the p element


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

The Code is as follows:


// Set the HTML code of the p element
$ ("P" ).html ("welcome to the concise modern magic library ~~ ");


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

Text () method

This method is similar to the innerText attribute in JavaScript. It can be used to read or leave the text content in an element empty. Continue to use the preceding HTML code and use the text () method to operate the p element:

The Code is as follows:


Var p_text = $ ("p"). text (); // obtain the text content of the p element.


Like the html () method, if you need to set text content for an element, you also need to pass a parameter. For example, to set text content for the p element, the Code is as follows:

The Code is as follows:


// Set the text content of the p element
$ ("P"). text ("welcome to the concise modern magic library ~~ ");


Pay attention to the following two points:

The innerText attribute in JavaScript cannot be run in the Firefox browser, while jQuery's text () method supports all browsers.
The text () method is effective for both HTML and XML documents.

When a friend list is displayed

Element settings:

In this way, js files can be dynamically obtained.

Element id, and the value $ ("# p" + userId). text (name) is successfully assigned through the text () method );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.