JS uses the HTML () or text () method to get the value of setting p label display _javascript tips

Source: Internet
Author: User

HTML () method

This method is similar to the innerHTML property in JavaScript, and can be used to read or set HTML content in an element. To get the contents of an element, you can do this:

var p_html = $ ("P"). html (); Get the HTML code for the P element

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

Set the P element's HTML code
$ ("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 property in JavaScript, which can be used to read or not place textual content in an element. Continue using the HTML code above, and use the text () method to manipulate the P element:

var P_text = $ ("P"). text (); Get the text content of the P element

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 set the text content for the P element, the code is as follows:

Sets the text content of the P element
$ ("P"). Text ("Welcome to the concise modern Magic Library ~ ~");

Be aware of the following two points:

The innertext attribute in JavaScript does not run in Firefox, and jquery's text () method supports all browsers.

The text () method is valid for both the HTML document and the XML document.

When displaying the buddy list, set the <p></p> element in the display buddy name: <p id= "P${friend.friendid}" ></p>, so that the JS file can be dynamically obtained <p> </p> ID of the element and successfully assigned $ ("#p" +userid) by the text () method. Text (name);

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.