Home >Web Front-end >JS Tutorial >Detailed explanation of jquery's method of operating object array elements_jquery

Detailed explanation of jquery's method of operating object array elements_jquery

WBOY
WBOYOriginal
2016-05-16 16:30:001420browse

The code is as follows:

Copy code The code is as follows:


a
b
c

1. Wrong method : You cannot use the [] method to get the jquery object array, as follows:

Copy code The 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 doesn’t work.

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

Copy code The code is 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 The code is as follows:

$(function() {
var div_span = $("#div1 span");
var i = 0;
div_span.each( function(){
            $(this).html(i);
          i ;
});
});

When each() traverses, if you use $(this), you will get a jquery object. If you use this directly, you will get a DOM object

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

The last three methods are correct, and the first one is wrong. I put him first because I want to emphasize that you can’t make the same mistake again in the future. Friends, please watch carefully.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn