Blogger Information
Blog 27
fans 0
comment 0
visits 17270
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
032-12月16日-JS第1节-变量、流程控制等
冇忉丼
Original
813 people have browsed it

案例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <label for="info">年龄:</label><input type="text" id="info">
  9. <button onclick="trans();">提交</button>
  10. <script>
  11. /********1、javascript变量、函数的定义**************/
  12. var a=70;
  13. var b=1.75;
  14. var bmi = function (){
  15. return a/(b*b);
  16. };
  17. var c = bmi();
  18. console.log(c);
  19. /********2、javascript流程控制if else switch**************/
  20. if(c<18.5){
  21. console.log('偏瘦');
  22. }else if(c>=18.5 && c<=24.9) {
  23. console.log('正常');
  24. }
  25. else if(c>24.9){
  26. console.log('超重');
  27. }
  28. //流程控制 switch
  29. switch (true) {//注意此处传的参数
  30. case c < 18.5:
  31. console.log('偏瘦');
  32. break;
  33. case c >= 18.5 && c <= 24.9:
  34. console.log('正常');
  35. break;
  36. default:
  37. console.log('超重');
  38. }
  39. /********3、javascript三种循环**************/
  40. //while循环
  41. while(a<80){
  42. a++;
  43. console.log(a);
  44. }
  45. //do...while循环
  46. do{
  47. a++;
  48. console.log(a);
  49. }while(a<80);
  50. //for循环
  51. for(a;a<80;a++){
  52. console.log(a);
  53. }
  54. //4、数据类型转换:parseInt、isNaN函数的使用
  55. function trans(){
  56. var age = document.getElementById('info').value;
  57. if(isNaN(age)){
  58. console.log('获取方法有误');
  59. }
  60. console.log(typeof(age));
  61. var age_num = parseInt(age);
  62. console.log(typeof(age_num));
  63. // var num = parseInt(age);
  64. // console.log(age_num+'年');
  65. }
  66. </script>
  67. </body>
  68. </html>

总结

js也为动态弱语言,定义时没有具体数据类型声明,只有关键字var或let.
函数,流程控制(ifelse,switch case),循环语句(for,while,do while)与php类似,php多了foreach循环数组这种循环形式。
typeof()的返回值为string.
输入数字判断isNaN(),转换为数字parseInt().

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post