Examples of different instances of the text () Val () and HTML () in jquery are detailed _jquery

Source: Internet
Author: User

Simply put: The difference between HTML () and text () is mainly the inclusion of labels. and Val () is for the form element.

But sometimes it's not so clear.

HTML (), Val (), text () is divided into arguments and no parameters.

Illustrate their differences:

HTML () Gets the contents of the first matching element without parameters. It's important to note that even if you match more than one, you can only get the first element that matches.
Such as:

<body>
<p> After you select this text, look at their text color and background colors, you can understand:: The role of selection. </p>
 
 

Here the con matches the 3 H3 elements, but only prints the contents of the first H3.

If we change this here to text (), we will print out three H3 content.

If with the parameter, con.html ("AAA"), and Con.text ("AAA"), same effect, can change the content of 3 H3.

But if con.html ("<span> aaa ' </span>") and Con.text ("<span> ' aaa ' </span>"), using HTML will parse span as a label, and text, it will be inserted as a string.

<body>
<p> After you select this text, look at their text color and background colors, you can understand:: The role of selection. </p>
 
 

HTML () will also remove the label, but text () will only get the textual part.

Look again Val ();

<body>
<input type= "text" value= "AAA"/>
<input type= "button" value= "buttons"/>
Select Gender:< Input type= "Radio" name= "Sex" checked value= "male" ><label> male </label> <input type= "Radio" name= "Sex"
"Value=" Women ><label> </label>
<br><br>
selected areas:
<select style= "width:150px ">
<option value=" 1 "> Shanghai </option>
<option value=" 2 "> Hangzhou </option>
< Option value= "3" > Nanjing </option>
<option value= "4" > Lijiang </option>
</select>
</body>
 
 

Here, you can focus on how to get the value of the radio button and how to get the value of the selected select.

It is important to note that option value is not what is displayed on the page, because setting or getting the option page content requires the use of text () and, of course, HTML ().

A summary of the differences between the three

1. Val () method and. HTML () is the same, and 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 them, and if. Text () is applied to more than one element, the text content of all selected elements will be read.

2. HTML (),. Text (),. Val () can dynamically change the contents of multiple elements by using the return value of the callback function.

The above is a small set of jquery to introduce you to the text () Val () and HTML () the difference between the examples, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.