Js uses the html () or text () method to obtain the value displayed for setting the p tag, jstext

Source: Internet
Author: User

Js uses the html () or text () method to obtain the value displayed for setting the p tag, jstext

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:

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:

// Set the HTML code for 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:

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:

// 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 the friend list is displayed, set <p id = "p $ {friend. friendId} "> </p>. In this way, the id of the <p> </p> element can be dynamically obtained in the js file () method value $ ("# p" + userId ). text (name );


How can I use js to retrieve the value of the html: text box?

Assume that the property value of the html: text label is abc, so the following can be written in js:
Document. forms [0]. abc. value

How to Use JS to get content in HTML tags

I don't know where I am
<Body>
<P id = "ss"> content </p> <input type = "button" name = "Submit" value = "show" onclick = "s ()"/>
</Body>
<Script>
Function s ()
{
Alert (document. getElementById ("ss"). innerHTML );
}
</Script>

Related Article

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.