Differences Between eq and get in jquery and their usage

Source: Internet
Author: User

For example:
<P style = "color: yellow"> Yu </p> uses eq to obtain the color value of the first p Tag:
$ ("P" ).eq(0).css ("color") // because eq (num) returns a jq object, therefore, you can use the jq method css to get the color value of the first p tag using get:
$ ("P "). get (0 ). style. color // because get (num) returns an html object, it is useless to use the traditional HTML object method. Of course, you can also get (num) and convert the object into the jq object before performing the operation:
$ ("P" ).get(0)).css ("color ")--------------------------------------------------------------------------
More eq
See:
Http://api.jquery.com/eq/
--------------------------------------------------------------------------
More get:
See:
Http://api.jquery.com/get/
Eq: The returned jquery object is used to reduce the Matching Element Set to an element. The position of this element in the Matching Element Set is changed to 0, and the set length is changed to 1.
Get: an html object array is used to obtain a matching element. Num indicates the number of matched elements.

For example, html code
Copy codeThe Code is as follows:
<Ul>
<Li> li-1 </li>
<Li> li-2 </li>
</Ul>

For example, if we use the jquery selector $ ("li"), then we will have two li elements. How can I select only one of them?

$ ("Li: eq (0)" ).html () or $ ("li" ).eq(0).html () is the first li here we will get the li-1
$ ("Li: eq (1)" ).html () or $ ("li" ).eq(1).html () is the second li here we will get li-2

Next let's take a look at get. Because get returns html objects, we are here
$ ("Li"). get (0). style. color = 'red'
You can only use this method or convert the get returned object to a jquery object in the operation.
$ ("Li" ).get(0)).css ("color", 'red ')

Complete code
Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Script language = "JavaScript" src = "js/jquery. js"> </script>
<Script language = "JavaScript">
<! --
$ ("Document"). ready (function (){
Alert ($ ("li: eq (1)" ).html (); // display li-2 alert ($ ("li: eq (0)" ).html ())
$ ("Li"). get (0). style. color = 'red ';
$ ("Li" ).get(1)).css ("color", 'red ')
})
// -->
</SCRIPT>
</HEAD>
<BODY>
<Ul>
<Li> li-1 </li>
<Li> li-2 </li>
</Ul>
</BODY>
</HTML>

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.