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

Source: Internet
Author: User

In jquery. HTML () is used as an HTML tag for reading and modifying elements,

. Text () is used to read or modify the plain text content of an 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.

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). Debug ()

3.VAL

Val (): Gets the current value of the first matching element.

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 ();

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

The comparison of the functions of the three methods

. HTML (),. Text (),. Val () Three methods are used to read the contents of the selected element; HTML () is used to read the HTML content of the element (including its HTML tags),. Text () is used to read the plain text content of the element, including its descendant elements,. Val () Is the "value" value that is used to read form elements. The. and. Text () methods cannot be used on form elements, whereas. val () can only be used on form elements; the. html () method only reads the first element when used on more than one element, and the. Val () method is the same as. html () If it is applied on more than one element. Only the value of the first FORM element can be read, but. Text () is different from theirs, and if. Text () is applied on more than one element, the textual content of all the selected elements is read.

. HTML (htmlstring),. Text (TextString), and. Val (value) Three methods are used to replace the contents of the selected element, and if three methods are applied on more than one element at the same time, the contents of all the selected elements will be replaced.

. HTML (),. Text (),. Val () can use the return value of the callback function to dynamically change the contents of multiple elements.

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

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.