jquery gets and sets the implementation code for the content or text of an HTML element _jquery

Source: Internet
Author: User
Tags php tutorial

First look at the demo effect:

Welcome to the cloud-dwelling community PHP tutorial

    • The cloud-dwelling community PHP tutorial
    • Easy to understand JavaScript magic
    • Easy to understand jquery magic

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 ("You are welcome to visit the cloud-dwelling community ~ ~"); 

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 cloud-dwelling Community Tour ~ ~");  

Be aware of the following two points:

The innertext attribute in 1.JavaScript does not run in Firefox, and the text () method of jquery supports all browsers.
The 2.text () method is valid for both the HTML document and the XML document.

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.