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.