Incorrect method for jquery to retrieve object array elements, jquery object array elements

Source: Internet
Author: User

Incorrect method for jquery to retrieve object array elements, jquery object array elements

The Code is as follows:

1 <div id = "div1"> 2 <span> a </span> 3 <span> B </span> 4 <span> c </span> 5 </div>

 

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

1 $ (function () {2 var div_span = $ ("# div1 span"); 3 for (var I = 0; I <div_span.length; I ++) {4 div_span.[ I }.html (I); 5} 6 });

This is invalid.

 

2. You can use jquery's eq () method to select:

1 for (var I = 0; I <div_span.length; I ++) {2 div_span.eq( I #.html (I); 3}

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

1 $ (function () {2 var div_span = $ ("# div1 span"); 3 var I = 0; 4 div_span.each (function () {5 blocks (this).html (I ); 6 I ++; 7}); 8 });

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

4. DOM object array obtained by pure js code. You can use [] to obtain array elements.

 

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.