Differences Between eq and get in jquery and their usage _ jquery

Source: Internet
Author: User
I believe most people will make mistakes in these two methods. You can view the API documentation carefully. Eq returns a jquery object, and get returns an array of html objects. For example:

Yu

Use 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

The Code is as follows:



  • Li-1

  • Li-2



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

The Code is as follows:





New Document

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.