The difference between get and EQ in jquery

Source: Internet
Author: User

The difference between get and EQ
. eq ()  Reduces the collection of matching elements, specifying the Index object precisely based on the index value. Get () gets the corresponding DOM element by retrieving the matching jquery object.

The same is the return element, so what is the difference between EQ and get?

eq Returns a jquery object, and get returns a DOM object . As an example:

$ ("Li"). Get (0). CSS ("Color", "red"); Error $ ("li"). EQ (0). CSS ("Color", "red"); That's right

The Get method essentially converts a jquery object into a DOM object, but the CSS belongs to the jquery constructor, and the DOM does not exist in this way, and if you need to use jquery, we have to write it this way:

var li = $ ("li"). Get (0); $ (LI). css ("Color", "red"); With $ packaging

Taking out the DOM object Li and then wrapping it again into a jquery object to call the CSS method is too cumbersome to write in 2 steps, so jquery provides us with a handy way to EQ ().

EQ () is implemented by turning the EQ method inside into a jquery object in the code above:

Eq:function (i) {    var len = this.length,        j = +i + (i < 0? len:0);    Return This.pushstack (J >= 0 && J < len? [This[j]]: []);

The logic of implementing code above is the same as get, the difference is that through the pushstack produced a new jquery object.

jquery is thoughtful and can only produce a new object through the EQ method, but what if a collection object needs to be handled? So jquery provides a slice approach:

Grammar:

. Slice (start [, end])

Role:

Filters the matching set of elements according to the specified subscript range, and generates a new JQuery object.

Because it is an array object, it means that we can use Silce to directly take the value, so we can write the code for the collection object:

var arr = []arr.push (This.slice (Start[,end]))     This.pushstack (arr)

This refers to the jquery object, because the jquery object is a collection of arrays, so we can take the number of collections directly from the native Silce method and then wrap them up.

Slice:function () {    return This.pushstack (slice.apply (this, arguments));},

The difference between get and EQ in jquery

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.