Differences between html, text, and val of jquery

Source: Internet
Author: User

Differences between html, text, and val of jquery

  1. . Html ()Use HTML tags for reading and modifying elements
  2. . Text ()The plain text content used to read or modify elements.
  3. . Val ()Used to read or modify the value of a form element.

    Functional Comparison of the three methods

    1. 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.
    2. . 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.
    3. . Html (),. text (),. val () can use the return value of the callback function to dynamically change the content of multiple elements.
    4. Example: 
    5. Here is a paragram

      Here is a big DIVfunction showVal () {var div = $ ("# div"); alert ("showVal ():" + div. val (); displayed as null} function showHtml () {var div =$ ("# div"); alert ("showHtml ():" nvidiv.html ()); shown:

      Here is a paragram

      Here is a big DIV} function showText () {var div = $ ("# div"); alert ("showText ():" + div. text (); displayed as: here is a paragram here is a big DIV}

      Conclusion: val () --- is generally used in input, instead of other elements, to obtain the input or select values.
      Html () and text () can be used on multiple elements, but html () is equivalent to returning based on the source code, that is, the returned content will contain various tags, while text () the returned content is the content between various tags.

      There are two methods in the html attribute: one with a parameter and the other without a parameter.
    6. 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:

      Hello

       

      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:

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

      Nice to meet you

      ");

       

      Result :[

      Nice to meet you

      ]

       

       

      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:

      HelloFine

       

       

      Thank you!

       

      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:

      Test Paragraph.

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

      Result :[

      SomeNew text.

      ]

       

       

      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

       

      Result:[

      Single:SingleMultiple:Multiple, Multiple3

      ]

       

      Example: // select multiple drop-down boxes, $ ('# multiple'). val () returns an array

       

      // $ ("# Multiple"). val (). join (",") to connect each value in the array

       

      Html page code:

       



      SingleSingle2
      MultipleMultiple2Multiple3

      Jquery code:

      ("P"). append ("Single:"+ $ (" # Single "). val () +"Multiple:"+ $ (" # Multiple "). val (). join (","));

       

      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:

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

      Result: hello world!


       

Related Article

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.