Javascript条件语句之if else
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>hello world</title> 6 </head> 7 <body> 8 <p>如果时间小于10:00,则生成问候“good morning,如果时间大于10:00小于20:00,则生成问候“good day”,否则生成问候“goodevening”</p> 9 <button onclick="myFunction()">点击这里</button> 10 <p id="demo"></p> 11 <script> 12 function myFunction(){ 13 var x=''; 14 var time= new Date().getHours(); 15 if (time<10){ 16 x ="good morning!" 17 } 18 else if(10<time<20){ 19 x ="good day" 20 21 } 22 else{ 23 x ="good evening" 24 } 25 document.getElementById("demo").innerHTML=x 26 } 27 28 </script> 29 </body> 30 </html>
Javascript之switch
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>JavaScript Switch</title> 6 </head> 7 <body> 8 <p>click this button to get witch day today</p> 9 <button onclick="myFunction()">click it</button> 10 <p id="demo"></p> 11 <script> 12 function myFunction(){ 13 var x; 14 var d = new Date().getDay(); 15 console.log(d) 16 switch (d){ 17 case 0:x="今天是星期日"; 18 break; 19 case 1:x="今天是星期一"; 20 break; 21 case 2:x="今天是星期二"; 22 break; 23 case 3:x="今天是星期三"; 24 break; 25 case 4:x="今天是星期四"; 26 break; 27 case 5:x="今天是星期五"; 28 break; 29 case 6:x="今天是星期六"; 30 break; 31 } 32 document.getElementById("demo").innerHTML=x 33 } 34 35 </script> 36 </body> 37 </html>
Javascript之for循环
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>JavaScript for 循环</title> 6 </head> 7 <body> 8 <p>cars is </p> 9 <script> 10 var cars = ["bms","volovo","byd","benz", "..."] 11 for (var i=0; i<cars.length; i++){ 12 document.write(cars[i], +'<br>') 13 console.log(cars[i]) 14 } 15 </script> 16 </body> 17 </html>
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>JavaScript for 循环啊</title> 6 </head> 7 <body> 8 <button onclick="myFunction()">click here to calculate it </button> 9 <p id="demo"></p> 10 <script> 11 function myFunction(){ 12 var x =""; 13 for (var i=0; i<5; i++){ 14 x = x + "number is "+ i + "<br>" 15 16 } 17 document.getElementById("demo").innerHTML=x 18 } 19 20 </script> 21 </body> 22 </html>
Javascript之for...in循环
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="uff-8"> 5 <title>JavaScript for/in 循环</title> 6 </head> 7 <body> 8 <script> 9 var person={ 10 fname:"Bill", 11 lname:"Gates", 12 age:56, 13 hobby:"sport" 14 } 15 for (x in person){ 16 txt = person[x] 17 console.log(txt) 18 } 19 20 </script> 21 </body> 22 </html>
Javascript之while循环
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>JavaScript while 循环啊</title> 6 </head> 7 <body> 8 <p>JavaScript while 循环啊</p> 9 <p> 10 while (条件){ 11 需要执行的代码块 12 } 13 </p> 14 <p>当i<10,打印所有值</p> 15 <button onclick="myFunction()">click here</button> 16 <p id="demo"></p> 17 <script> 18 function myFunction(){ 19 var x='', i=0; 20 while (i<10){ 21 x = x +"this number is " + i + "<br>"; 22 i++ 23 } 24 document.getElementById("demo").innerHTML=x 25 } 26 </script> 27 28 </body> 29 </html>
JavaScript之do...while循环
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>JavaScript do while</title> 6 </head> 7 <body> 8 <p> 9 语法:do/while 循环是 while 循环的变体。该循环会在检查条件是否为真之前执行一次代码块,然后如果条件为真的话,就会重复这个循环。 10 do { 11 需要执行的代码块 12 } 13 while(条件); 14 </p> 15 <p>点击下面按钮,只要i小于5就一直循环代码</p> 16 <button onclick="myFunction()">点击这里</button> 17 <p id="demo"></p> 18 <script> 19 function myFunction(){ 20 var x="", i=0 21 do { 22 x = x + "该数字为"+ i +"<br>"; 23 i++ 24 } 25 while (i<5) 26 document.getElementById("demo").innerHTML=x 27 } 28 </script> 29 </body> 30 </html>
JavaScript之for循环遍历数组
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>JavaScript for 循环遍历数组值</title> 6 </head> 7 <body> 8 <p>for 循环打印数组所有值啊</p> 9 <button onclick="myFunction()">点击这里打印所有值</button> 10 <p id="demo"></p> 11 <script> 12 function myFunction(){ 13 //定义一个数组 14 cars=["BMW","Volvo","Saab","Ford"]; 15 //定义一个自增变量i=0 16 var i=0 17 for (;cars[i];){ 18 document.write(cars[i]+"<br>"); 19 i++ 20 } 21 } 22 </script> 23 </body> 24 </html>
JavaScript条件语句之while循环遍历数组
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>JavaScript while 循环遍历数组值</title> 6 </head> 7 <body> 8 <p>while 循环遍历数组值啊</p> 9 <button onclick="myFunction()">点击这里遍历所有值</button> 10 <p id="demo"></p> 11 <script> 12 function myFunction(){ 13 //定义两个变量 14 var cars=["BMW","Volvo","Saab","Ford"]; 15 i=0; 16 while (cars[i]){ 17 document.write(cars[i]+"<br>"); 18 i++ 19 } 20 21 } 22 </script> 23 </body> 24 </html>
标签:语句,10,JavaScript,11,Javascript,while,循环,条件,var From: https://www.cnblogs.com/wenzm/p/17022391.html