A brief analysis of the HTML (), text (), Val () difference in jquery (_jquery)

Source: Internet
Author: User
Tags html tags

1.HTML

HTML (): Gets the HTML content of the first matching element. This function is not available for XML documents. But can be used for XHTML documents

HTML (val): Sets the HTML content for each matching element. This function is not available for XML documents. But can be used for XHTML documents.

2.TEXT

Text (): Gets the contents of all matching elements.

The result is text that is combined with the text content contained by all matching elements. This method works for both HTML and XML documents.

Text (val): Sets the textual content of all matching elements

Similar to HTML (), but will encode HTML (replace "<" and ">" with the corresponding HTML entity).

3.VAL

Val (): Gets the current value of the first matching element.

Val (val): Sets the value of each matching element.

The above content is copied in the jquery Help document, and no more nonsense to say. Here are some of the exercises you've done, the code reads as follows:

I found another different place for HTML and text when I was doing the exercise.

When HTML () goes to the contents of an element, it is able to take the format of the selected element below.

such as: <div id= "Divshow" ><b><i>write less do more</i></b></div>

If we use var strhtml = $ ("#divShow"). html ();

The result: <b><i>write less do more</i></b>

If we use var strHTML2 = $ ("#divShow b I"). html ();

The result is write less do

and text does not have the first case,

If we var StrText = $ ("#divShow"). Text ();

The result is write less do

<%@ 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" >  

Everyone can also go to the verification, the above is my experiment, I use jquery is 1.6

To sum up:

. HTML () is used for HTML tags that read and modify elements
. text () to read or modify the plain text content of an element
. val () is used to read or modify the value values of the form elements.

The functional comparisons of these three methods

. HTML (),. Text (),. Val () Three methods are used to read the contents of the selected element, except that. html () is the HTML content (including its HTML tag) that is used to read the element, and. Text () is used to read the plain text content of the element, including its descendant elements,. Val () is used to read the value of the form element. The. and. Text () methods cannot be used on form elements. and. Val () can only be used on form elements, and when the. html () method is used on more than one element, only the first element is read; the. Val () method is the same as. html (), 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 is read.

. HTML (htmlstring),. Text (TextString) and. Val (value) Three methods are used to replace the contents of a selected element, and if three methods are applied to multiple elements at the same time, the contents of all selected elements will be replaced.

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

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.