Home Web Front-end JS Tutorial Summary of jquery operation object array element methods (with case)

Summary of jquery operation object array element methods (with case)

Apr 24, 2018 am 09:41 AM
jquery element array

This time I bring you jqueryOperationObjectArrayElement method summary (with case), jqueryNotes on operating object array elements What are they? The following is a practical case. Let’s take a look.

 <p id="p1">
     <span>a</span>
     <span>b</span>
     <span>c</span>
 </p>
Copy after login

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

 $(function() {
     var p_span = $("#p1 span");
     for( var i = 0; i < p_span.length; i++ ) {
         p_span.[i].html(i);
     }
 });
Copy after login

This is invalid.

2.YesUse jquery's eq() method to select:

 for( var i = 0; i < p_span.length; i++ ) {
     p_span.eq(i).html(i);
 }
Copy after login

3. You canuse the each() method to traverse:

 $(function() {
     var p_span = $("#p1 span");
     var i = 0;
     p_span.each( function(){
         $(this).html(i);
         i++;
     });
 });
Copy after login

each() traverse When using $(this), you get the jquery object. If you use this directly, you get the DOM object

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

The last three methods are correct, the first one is wrong, I put it first because I want to emphasize that it cannot be done in the future. If you make the same mistake again, friends, please watch carefully.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

jquery’s method grep() implements array filtering

How jquery traverses arrays

The above is the detailed content of Summary of jquery operation object array element methods (with case). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to remove duplicate elements from PHP array using foreach loop? How to remove duplicate elements from PHP array using foreach loop? Apr 27, 2024 am 11:33 AM

How to remove duplicate elements from PHP array using foreach loop?

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

PHP array key value flipping: Comparative performance analysis of different methods

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy May 01, 2024 pm 12:30 PM

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy

PHP array multi-dimensional sorting practice: from simple to complex scenarios PHP array multi-dimensional sorting practice: from simple to complex scenarios Apr 29, 2024 pm 09:12 PM

PHP array multi-dimensional sorting practice: from simple to complex scenarios

Application of PHP array grouping function in data sorting Application of PHP array grouping function in data sorting May 04, 2024 pm 01:03 PM

Application of PHP array grouping function in data sorting

Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Apr 30, 2024 pm 03:42 PM

Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods

The role of PHP array grouping function in finding duplicate elements The role of PHP array grouping function in finding duplicate elements May 05, 2024 am 09:21 AM

The role of PHP array grouping function in finding duplicate elements

PHP array merging and deduplication algorithm: parallel solution PHP array merging and deduplication algorithm: parallel solution Apr 18, 2024 pm 02:30 PM

PHP array merging and deduplication algorithm: parallel solution

See all articles