jquery Operations Object Array element method detailed _jquery

Source: Internet
Author: User

The code is as follows:

Copy Code code as follows:

<div id= "Div1" >
<span>a</span>
<span>b</span>
<span>c</span>
</div>

1. Wrong way : cannot use [] way to take jquery object array, as follows:

Copy Code code as follows:

$ (function () {
var Div_span = $ ("#div1 span");
for (var i = 0; i < div_span.length; i++) {
Div_span. [I].html (i);
}
});

This is not valid.

2. a jquery eq () method can be used to select:

Copy Code code as follows:

for (var i = 0; i < div_span.length; i++) {
Div_span.eq (i). html (i);
}

3. You can use each () method to traverse :

Copy Code code as follows:

$ (function () {
var Div_span = $ ("#div1 span");
var i = 0;
Div_span.each (function () {
$ (this). HTML (i);
i++;
});
});

When each () is traversed, if you get a jquery object with $ (this), if you use this directly, you get a DOM object

4. The pure JS code obtains the DOM object array , may obtain the array element by [] the way

The back 3 is the right way, the first one is wrong, put him in the first, is because to emphasize, the future can not make the same mistake, the small partners can be seen carefully ha.

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.