• js--小结⑤


    js中的for循环,while循环,do...while循环和C语言的一模一样

    有几个问题要提醒一下的是

    1.  null是对象,即object       undefined是undefined

    demo

     1 <!DOCTYPE html>
     2 <html>
     3 <body>
     4 
     5 <p id="demo"></p>
     6 
     7 <script>
     8 document.getElementById("demo").innerHTML =
     9 typeof undefined + "<br>" +
    10 typeof null + "<br>" +
    11 (null === undefined) + "<br>" +
    12 (null == undefined);
    13 </script>
    14 
    15 </body>
    16 </html> 
    17             

    结果:

    2.几个demo

    break的标签引用

     1 <!DOCTYPE html>
     2 <html>
     3 <body>
     4 
     5 <script>
     6 cars=["BMW","Volvo","Saab","Ford"];
     7 list:
     8 {
     9 document.write(cars[0] + "<br>"); 
    10 document.write(cars[1] + "<br>"); 
    11 document.write(cars[2] + "<br>"); 
    12 break list;
    13 document.write(cars[3] + "<br>"); 
    14 document.write(cars[4] + "<br>"); 
    15 document.write(cars[5] + "<br>"); 
    16 }
    17 </script>
    18 
    19 </body>
    20 </html>            

    do-while

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Document</title>
     6 </head>
     7 <body>
     8     <p>点击下面的按钮,主要i小于5就一直循环代码块</p>
     9     <button onclick="myFunction()">hit me</button>
    10     <p id="demo"></p>
    11     <script>
    12     function myFunction()
    13     {
    14         var x="",i=0;
    15         do{
    16             x=x+"The number is " +i+ "<br />";
    17             i++;
    18         }        
    19         while(i<5)
    20          document.getElementById("demo").innerHTML=x;
    21         
    22     }
    23     </script>
    24     
    25 </body>
    26 </html>

    结果:

    while

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Document</title>
     6 </head>
     7 <body>
     8     <p>点击下面的按钮,只要i小于5就一直循环代码块</p>
     9     <button onclick="myFunction()">hit me</button>
    10     <p id="demo"></p>
    11     <script>
    12     function myFunction()
    13     {
    14         var x="",i=0;
    15         while(i<5)
    16         {
    17             x=x+"The number is " +i+ "<br />";
    18             i++;
    19         }
    20         document.getElementById("demo").innerHTML=x;
    21     }
    22     </script>
    23 </body>
    24 </html>

    结果

    for

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Document</title>
     6 </head>
     7 <body>
     8     <script>
     9     cars=["BMW","Volvo","Saab","Ford"];
    10     var i=0;
    11     for(;cars[i];)
    12     {
    13         document.write(cars[i]+"<br />");
    14         i++;
    15     }
    16     </script>
    17 </body>
    18 </html>

    结果

    break

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Document</title>
     6 </head>
     7 <body>
     8     <p>点击按钮,测试带有break语句的循环</p>
     9     <button onclick="myFunction()">hit me</button>
    10     <p id="demo"></p>
    11     <script>
    12     function myFunction()
    13     {
    14         var x="",i=0;
    15         for(i=0;i<10;i++)
    16         {
    17             if(i==3)
    18             {
    19                 break;
    20             }
    21             x=x+"The numbe is "+i+ "<br />";
    22 
    23         }
    24         document.getElementById("demo").innerHTML=x;
    25     }
    26     </script>
    27 </body>
    28 </html>

    continue

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Document</title>
     6 </head>
     7 <body>
     8     <p>点击下面的按钮来执行循环,该循环会跳过i=3的步进</p>
     9     <button onclick="myFunction()">hit me</button>
    10     <p id="demo"></p>
    11     <script>
    12     function myFunction()
    13     {
    14         var x="",i=0;
    15         for(i=0;i<10;i++)
    16         {
    17             if(i==3)
    18             {
    19                 continue;
    20             }
    21             x=x+"the numeber is "+i +"<br />";
    22             document.getElementById("demo").innerHTML=x;
    23         }
    24 
    25     }
    26     </script>
    27 </body>
    28 </html>

    结果:

    ---- 动动手指关注我!或许下次你又能在我这里找到你需要的答案!ZZZZW与你一起学习,一起进步!
  • 相关阅读:
    sybase数据库破解
    原生js实现音乐列表(隔行换色、全选)
    原生JavaScript实现评分效果
    原生javascript实现选项卡(基础版)
    LeetCode【111. 二叉树的最小深度】
    LeetCode【110. 平衡二叉树】
    LeetCode【108. 将有序数组转换为二叉搜索树】
    LeetCode【101. 对称二叉树】
    LeetCode【104. 二叉树的最大深度】
    持续更新scrapy的错误,ValueError: Missing scheme in request url:
  • 原文地址:https://www.cnblogs.com/zzzzw/p/4598373.html
Copyright © 2020-2023  润新知