Examples of differences between html (), text (), and val () in JQuery: jqueryval

Source: Internet
Author: User

Examples of differences between html (), text (), and val () in JQuery: jqueryval

1. HTML

Html (): gets the html content of the First Matching Element. This function cannot be used in XML documents. But it can be used in XHTML documents.
Html (val): sets the html content of each matching element. This function cannot be used in XML documents. But it can be used in XHTML documents.

2. TEXT

Text (): Get the content of all matching elements.
The result is a combination of text content contained by all matching elements. This method is effective for both HTML and XML documents.
Text (val): Set the text content of all matching elements.
Similar to html (), but the encoding HTML (replace "<" and ">" with the corresponding HTML Entity ).

3. VAL

Val (): obtains the current value of the First Matching Element.
Val (val): sets the value of each matching element.

The above content is copied in the help document of JQuery, and it is not nonsense. Below are some exercises you have made. The Code is as follows:
During the exercises, I found another difference between html and text.
When html () is used to remove the content of an element, the format below the selected element can also be obtained.
For example, <div id = "divShow"> <B> <I> Write Less Do More </I> </B> </div>
If we use var strHTML = $ ("# divShow" cmd.html,
Result: <B> <I> Write Less Do More </I> </B>
If we use var strHTML2 =$ ("# divShow B I" cmd.html ();
The result is Write Less Do More.
Text does not have the first case,
If var strText = $ ("# divShow"). text ();
The result is Write Less Do More.

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

You can also verify it by yourself. The above is my experiment, and the JQuery I use is 1.6


In jquery, html (), text (), and val () are different.

Html means you can add tags such as <a> </a> and <p> </p>.
Only text can be written. If the above mark is written, it will be output as text.
Val is an attribute. Only objects with this attribute can be called.

What are the differences between text (), html (), and val () in jQuery?

Text (): obtains or modifies the text of a specified element.
Html (): gets or modifies the html elements and text of a specified element.
Val (): Get or change the value of a specified Element (usually a form element)

The above three are all the syntaxes in the jquery class library.

The second problem is that there is basically no difference. The function is to obtain the value of the current object (usually a form element)
However, this. value is the native Syntax of js, and $ (this). val () is the syntax of jquery.
Using this. value removes the need to introduce any library files, while $ (this). val () requires the introduction of jquery library files.

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.