Relationship and difference between values of html, val, and text attributes in jquery

Source: Internet
Author: User

First, there are two methods in the html attribute: one with a parameter and the other without a parameter.

1. No parameter html (): obtains the html content of the First Matching Element. This function cannot be used in XML documents. But it can be used in XHTML documents and returns a String

Example:

Html page code: <div> <p> Hello </p> </div>

Jquery code: $ ("div" ).html ();

Result: Hello.

2. 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. Returns a jquery object.

Html page code: <div> </div>

Jquery code: $ ("div" ).html ("<p> Nice to meet you </p> ");

Result: [<div> <p> Nice to meet you </p> </div>]

Second, there are two methods in the text attribute: one with a parameter and the other without a parameter.

1. No parameter text (): obtains the content of all matching elements. The result is a combination of text content contained by all matching elements. Returns a String

Example:

Html page code: <p> <B> Hello </B> fine </p>

<P> Thank you! </P>

Jquery code: $ ("p"). text ();

Result: HellofineThankyou!

2. text (val): Set the text content of all matching elements, similar to html, however, encode HTML (replace "<" and ">" with the corresponding HTML Entity ). returns a jquery object.

Html page code: <p> Test Paragraph. </p>

Jquery code: $ ("p"). text ("<B> Some </B> new text .");

Result: [<p> <B> Some </B> new text. </p>]

Finally, there are two methods in the val () attribute, one with a parameter and the other without a parameter.

1. No parameter val (): obtains the current value of the First Matching Element. In jQuery 1.2, the value of any element can be returned. Including select. If multiple values are selected, an array containing the selected values is returned.

Returns a String, array

Example:

Html page code:
Copy codeThe Code is as follows:
<P> </p> <br/>
<Select id = "single">
<Option> Single </option>
<Option> Single2 </option>
</Select>
<Select id = "multiple" multiple = "multiple">
<Option selected = "selected"> Multiple </option>
<Option> Multiple2 </option>
<Option selected = "selected"> Multiple3 </option>
</Select>

Jquery code: $ ("p "). append ("<B> Single: </B>" + $ ("# single "). val () + "<B> Multiple: </B>" + $ ("# multiple "). val (). join (","));

Result: [<p> <B> Single: </B> Single <B> Multiple: </B> Multiple, Multiple3 </p>]

2. val (val): sets the value of each matching element. In jQuery 1.2, this can also assign values to check, select, and radio elements, and return a jquery object.

Html page code: <input type = "text"/>

Jquery code: $ ("input"). val ("hello world! ");

Result: hello world!

-----------------------------------------------------------------

Summary:

1. if an option value is assigned to the <select> label in the drop-down box, <option name = "", value = ""> </option> although option has the value attribute, however, the value attribute of option on the page is not displayed on the page, but the text fields between option labels can be displayed on the page. Therefore, to assign values to the option tag, use the html () method.

2. if you assign values to the <input id = "pro" name = "province" value = "Anhui"> tag, because the value attribute in the input tag is displayed on the page, therefore, if you assign values to the text of the Input tag and display the text value in the browser during browser parsing, you must use val () and attribute.

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.