jquery gets text node ()/val ()/html () method difference

Source: Internet
Author: User

What is the difference between val,text,html in jquery and the ability to add a parameter to a value? Let's take a few examples to illustrate:
First, there are two methods in the HTML attribute, one with an argument, one without
1. No parameter HTML (): Gets the HTML content of the first matching element. This function cannot be used with XML documents. But it can be used in an XHTML document, which returns a string
Example:
HTML page Code:<div><p>hello</p></div>
jquery Code: $ ("div"). html ();
Result: Hello
2. Parameter HTML (val): Sets the HTML content for each matching element. This function cannot be used with 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>");
Results: [<div><p> nice to meet you</p></div>]

Second, there are two methods in the Text property, one with a parameter, one without a parameter
1. No parameter text (): Gets the contents of all matching elements. The result is text that is combined with the text content contained by all matching elements. Returned is a string
Example:
HTML page Code:<p><b>hello</b> fine</p>
<p>thank you!</p>
jquery Code: $ ("P"). text ();
Results: hellofinethankyou!

2. Parameter text (val): Sets the text content of all matching elements, similar to HTML (), but encodes the HTML (replaces "<" 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.");
Results: [<p><b>Some</b> new text.</p>]

Finally, there are two methods in the Val () attribute, one with a parameter and one without a parameter.
1. No parameter Val (): Gets the current value of the first matching element. In JQuery 1.2, you can return the value of any element. Includes select. If multiple selections, an array is returned that contains the selected value.
Returns a string, array
Example:
HTML page code:


The 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 (", "));
Results: [<p><b>single:</b>single<b>multiple:</b>multiple, Multiple3</p>]
2. Parameter Val (val): Sets the value of each matching element. In jquery 1.2, this can also be assigned a value for the Check,select,radio element, returning a JQuery object
HTML page code:
<input type= "Text"/>
jquery Code: $ ("input"). val ("Hello world!");

Result: Hello world!

The difference between the use of HTML () and Val () in jquery:
1). The difference between HTML () and Val ()
$ ("#myId"). HTML ("abc");
If myID does not exist the above code will not be error, just do not do any action;
var data=$ ("#myId"). html ();
If myID does not exist, data is a null value
var data=$ ("#myId"). Val ();
If myID does not exist, data is undefined

2). How jquery determines that an element exists
jquery Selector Get element returns an object regardless of whether the element exists
var my_element=$ ("#myId");
Here, regardless of whether the element "myID" exists, My_element is always an object
Use the following method to determine whether the "myID" element exists
if (my_element.length>0)) {
Alert ("exist");
}else{
Alert ("not Found");

jquery gets text node ()/val ()/html () method difference

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.