Home > Web Front-end > JS Tutorial > Detailed explanation of basic usage examples of jquery's each loop

Detailed explanation of basic usage examples of jquery's each loop

伊谢尔伦
Release: 2017-06-16 10:33:48
Original
1744 people have browsed it

Common loop statements in javascript are: whileloop do whileloop and forLoop . All three types of loop statements have their own advantages.

1. While loop

Format As follows:

## while (condition){

Code that needs to be executed

}

document.write("Here is the input of 1-50 printed by while:");

var i = 1;

while(i<=50){

document.write(i);

i++;

}

2. do while loop

The format is as follows:

do{

Loop body 2

}while(logical judgment 1);

                                                                                                                                                                                                                                                                                                  # ");

var k = 1;

do{

document.write( k);

k++;

}while(k<=50);

3. for loop

The format is as follows:

for (initialization (1); logical judgment (2); Assignment (3)){

Loop body (4)

}

Execution process Is: 1--->2--->4-->3

When does the for loop end: When the logical judgment is false (false), This terminates the cycle.

Example: Print from 1 to 100;

for(var i=1;i<=100;i++){

document.wirter(" "+i)

}

##The difference between the three is demonstrated by a small case:

do{

##document.write("Will this be entered into the page?");

}while(1==2);

for(var i=1;i==2;i++){

document .write("Will for be entered into the page here?");

}

while(i==2){

document.write("While will this be input to the page?");

}

Proved by actual practice: the content in the do while statement will be output directly.

Summary: The difference between for loop while loop do while loop is that: do while will execute the loop body once regardless of whether the logical conditions are met, while while and for But it won't.

Attached jquery each() loop-try it yourself

      <html>
    <head>
    <script type="text/javascript" src="/jquery/jquery.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
      $("button").click(function(){
        $("li").each(function(){
          alert($(this).text())
        });
      });
    });
    </script>
    </head>
    <body>
    <button>输出每个列表项的值</button>
    <ul>
    <li>Coffee</li>
    <li>Milk</li>
    <li>Soda</li>
    </ul>
    </body>
    </html>
Copy after login



The above is the detailed content of Detailed explanation of basic usage examples of jquery's each loop. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template