Read jquery Five (take DOM element) _jquery

Source: Internet
Author: User
After the $ call for jquery you want to get DOM elements you can use the Getting method, as follows
Copy Code code as follows:

Mode 1
$ (' div '). Get (1); Get the second div on a page

Of course, you can also use array indexing to get
Copy Code code as follows:

Mode 2
$ (' div ') [1]; Get the second div on a page

You can get a particular DOM element in both of these ways, while getting a collection of DOM elements uses the ToArray method
Copy Code code as follows:

$ (' div '). ToArray (); Return all the div in the page and put it in the array in turn

Look at the source of the Get method
Copy Code code as follows:

Get:function (num) {
return num = null?
Return a ' clean ' array
This.toarray ():
Return just the object
(Num < 0 this[this.length + num]: this[num]);
},

A GET has one ternary operator, or two branches.
Branch 1, which gets all the DOM elements (call ToArray) when no arguments are passed
Branch 2, when NUM is a number (index), returns one of the specified DOM elements (in reverse when the number is negative)

Branch 1 actually turns the jquery object (pseudo array) into a real array, and branch 2 actually takes the "pseudo array" element (in brackets []). The entire call process is described below with $ (' div ')
1, get the page div element (document.getelementsbytagname (' div '))
2, turn the collection htmlcollection/nodelist into a real array
3, convert array to pseudo array/arraylike (jquery object)
As shown in figure


The first step is what the selector does;
The second step is to htmlcollection/nodelist the number of groups previously discussed;
In the third step, the array is converted to a pseudo array/arraylike (jquery object), which simply calls the push of the next array.
Perhaps the following examples are easy to understand

Copy Code code as follows:

var jqobj = {0: ' One ', 1: ' Two ', 2: ' Three ', length:3},//pseudo Array (arraylike)
ary = [' One ', ' two ', ' three ']; Array
To arraylike a pseudo array (a) to a group
function Jqobjtoarray (JSON) {
var slice = Array.prototype.slice;
Return Slice.call (json,0);
}
Convert an array to a pseudo array (arraylike)
function Arraytojqobj (ary) {
var obj = {}, push = Array.prototype.push;
Push.apply (obj,ary);
return obj;
}
Console.log (Jqobjtoarray (jqobj));
Console.log (arraytojqobj (ary));

jquery also provides the First/last/eq/slice method, which differs from the Get/toarray mentioned above. They return a jquery object instead of returning to HtmlElement. The following HTML fragment
Copy Code code as follows:

<div id= "a" >A</div>
<div id= "B" >B</div>
<div id= "C" >C</div>
<div id= "D" >D</div>

$ (' div '). First () returns the JQ object collection, Div[id=a], structure: {0:div,length:1,...} ;//... Indicates that other attributes are omitted
$ (' div '). Last () returns the final element of the collection of JQ objects, i.e. Div[id=d], structure: {0:div,length:1,...} ;
$ (' div '). EQ (2) returns the third element matched by the JQ object, i.e. Div[id=c], structure: {0:div,length:1,...} ;

Check the source to know:
The EQ method is called directly in the 1,first,last method.
The slice method is called in the 2,eq method.

So the slice method is fundamental. This approach allows us to naturally think of array slice methods, in fact jquery is the use of array of push and slice methods.
1, using the Array.prototype.slice method to transfer pseudo-array to a group
2, using the Array.prototype.push method to convert an array to a pseudo array
Of course, the slice method in jquery invokes Pushstack. I have abbreviated the slice here, as follows
Copy Code code as follows:

Slice:function () {
var ret = $ ();//key sentence, construct a new JQ object,
var ary = slice.apply (this,arguments);//This is the JQ object, which is an array of subsets based on the parameters.
Push.apply (ret,ary)//Turn into JQ object, pseudo array form
return ret;
},

Related:

Converts a htmlcollection/nodelist/pseudo array to a group
Zchain-05.rar

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.