Jquery operation object array element method details, jquery Array

Source: Internet
Author: User

Jquery operation object array element method details, jquery Array

The Code is as follows:

Copy codeThe Code is as follows:
<Div id = "div1">
<Span> a </span>
<Span> B </span>
<Span> c </span>
</Div>

1. Incorrect Method: The jquery object array cannot be obtained using the [] method, as shown below:

Copy codeThe Code is as follows:
$ (Function (){
Var div_span = $ ("# div1 span ");
For (var I = 0; I <div_span.length; I ++ ){
Div_span.[ I #.html (I );
}
});

This is invalid.

2. YesUsing jquery's eq () methodTo select:

Copy codeThe Code is as follows:
For (var I = 0; I <div_span.length; I ++ ){
Div_span.eq( I ).html (I );
}

3. YesUse the each () method to traverse:

Copy codeThe Code is as follows:
$ (Function (){
Var div_span = $ ("# div1 span ");
Var I = 0;
Div_span.each (function (){
Certificate (this).html (I );
I ++;
});
});

During each () traversal, if $ (this) is used to obtain the jquery object, if this is used directly, the DOM object is obtained.

4DOM object array obtained by. js Code only, You can use [] to obtain array elements.

The next three methods are correct. The first is wrong. Put him in the first place because we must emphasize that we cannot make the same mistake again in the future. Let's take a closer look.

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.