Differences between html, text, and val in jquery

Source: Internet
Author: User
Tags html page html tags

. Html () uses HTML tags 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 a form element.

I. HTML in jquery
 
1. No parameter html ()
Token is used to obtain the HTML content of any element. If you call multiple optional element's. HTML () method, the first element is read. In other words, if the selector matches more than one element, only the HTML content of the first matching element is obtained. In addition, this method is invalid for XML files.
To read the HTML content of a element, you must first select the element and then use the .html () method in jquery.
 
2. html (val)
Set 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.


Text in jquery
 
1. No parameter text ()
Obtain the content of all matching elements. The result is a combination of text content contained by all matching elements. Returns a String.
Text () combines the text content of each element in the element set that matches the element, including their descendants .. The text(watermark and .html () methods are different. The. text () method can be used in XML and HTML documents .. The text () method is composed of all the text content contained in the matching element (because different browsers have different HTML analyzer, the line feed in the returned text may be different from other spaces .)
 
2. Parameter text (val)
Set the text content of all matching elements, similar to html (), but encode HTML (replace "<" and ">" with the corresponding HTML entity). Return a jquery object.


Val in jquery
 
1. No parameter val ()
Obtain the content of all matching elements. The result is a combination of text content contained by all matching elements. Returns a string or array.
The. val () method is mainly used to obtain the values of form elements. For the "<select multiple =" multiple ">" element ,. the val () method returns an array containing each selected option. For the following selection box "<select>" and check box, single choice ([type = "checkbox"], [type = "radio"]) you can use the ": selected" and ": checked" selectors to obtain the value.
 
2. Parameter text (val)
Set the text content of all matching elements, similar to html (), but encode HTML (replace "<" and ">" with the corresponding HTML entity). Return a jquery object.
This method is often used to set the value of the form field. For the "<select multiple =" multiple ">" element, multiple options can be selected through an array .. Val (value) can change the value of the selected element at the same time, and its value is the same

Functional comparison of the three methods

The three types of content .html (,.text(),.val()..) are used to read and select elements. Only the content .html () is used to read the HTML content of elements (including its Html tags ),. text () is used to read the plain text content of an element, including its child element ,. val () is the "value" value used to read form elements. Except () is the same. If it is applied to multiple elements, it can only read the "value" value of the first form element,. text () is different from them, if. when text () is applied to multiple elements, the text content of all selected elements is read.
. Html (htmlString ),. text (textString) and. val (value) is used to replace the content of the selected element. If the three methods are applied to multiple elements at the same time, the content of all selected elements will be replaced.
. Html (),. text (),. val () can use the return value of the callback function to dynamically change the content of multiple elements.


Differences between html, text, and val

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:
<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!

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.