The HTML (), text (), Val () difference in jquery

Source: Internet
Author: User

In jquery. HTML () is used for reading and modifying elements of the HTML tag,. Text () to read or modify the plain text content of the element,. Val () is used to read or modify the value of the form element.

1.HTML

HTML (): Gets the HTML content of the first matching element. This function cannot be used with XML documents. But can be used in XHTML documents

HTML (val): Sets the HTML content of each matching element. This function cannot be used with XML documents. But it can be used in XHTML documents.

2.TEXT

Text (): Gets the contents of all matching elements. General with ID difference

The result is text that is combined with the text content contained by all matching elements. This method is valid for both HTML and XML documents.

Text (val): Sets the textual content of all matching elements

Similar to HTML (), but encodes HTML (replaces "<" and ">" with the corresponding HTML entity).

3.VAL

Val (): Gets the current value of the first matching element. Typically used to take the value of input.

Val (val): Sets the value of each matching element.

The above content is copied in the jquery Help document, and no more talking nonsense. Here are some of your own exercises, the code is as follows:

I found another different place in HTML and text when I was doing the exercises.

HTML () to the content of the element, you can select the element below the format is also taken.

such as: <div id= "Divshow" ><b><i>write less do more</i></b></div>

If we use var strHTML = $("#divShow").html(); them,

The result is: <b><i>write less do more</i></b>

If we use var strHTML2 = $ ("#divShow b I"). html ();

The result is write less does more

And the text doesn't have the first case,

If we var StrText = $ ("#divShow"). Text ();

The result is write less does more

If we var StrText = $ ("#divShow"). Val ();

The result is ""

The HTML (), text (), Val () difference in jquery

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.