Blogger Information
Blog 42
fans 5
comment 0
visits 38505
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1216:Javascript基础
张浩刚
Original
650 people have browsed it
  1. <script>
  2. //变量
  3. var a = '张明轩';
  4. console.log(a);
  5. //for循环
  6. for(var i=0;i<10;i++){
  7. console.log(i);//输出1-9
  8. }
  9. //while循环
  10. var i=0;
  11. while(i<10){
  12. i++; //输出0-10
  13. console.log('while'+i);
  14. i++; //输出0-9
  15. }
  16. //do while 循环
  17. var i=0;
  18. do{
  19. i++;
  20. console.log('do'+i);//输出 1-10;先执行后判断,故含10
  21. }while(i<10)
  22. //函数与变量
  23. var abc = '张明轩';
  24. function show()
  25. {
  26. var abc = '张明朗';
  27. function run()
  28. {
  29. console.log(abc); //张明朗
  30. }
  31. run();
  32. console.log(abc); //张明朗
  33. }
  34. show();
  35. console.log(abc); //张明轩
  36. //三元 三目运算符
  37. var num1 = 10;
  38. var num2 = 20;
  39. console.log(num1>num2 ? 'num1大于num2' : 'num1小于num2');
  40. //函数与判断
  41. function show1()
  42. {
  43. $result = 70
  44. if($result>90){
  45. console.log('优秀');
  46. }elseif($result>80 && $result<=90){
  47. console.log('良好');
  48. }else if($result>60 && $result<=80){
  49. console.log('合格');
  50. }else{
  51. console.log('差');
  52. }
  53. }
  54. show1();
  55. function show2()
  56. {
  57. var myscore = 71;
  58. switch( true ){
  59. case myscore>90:console.log('优秀');
  60. break;
  61. case myscore<=90 && myscore>70:console.log('良好');
  62. break;
  63. case myscore<=70 && myscore>60:console.log('及格');
  64. break;
  65. default:console.log('不及格');
  66. }
  67. }
  68. show2();
  69. //推荐使用一目了然模式
  70. function show3()
  71. {
  72. if(myscore>90){
  73. console.log('优秀');
  74. }
  75. if(myscore<=90 && myscore>70){
  76. console.log('良好');
  77. }
  78. if(myscore<=70 && myscore>=60){
  79. console.log('及格');
  80. }
  81. if(myscore<60){
  82. console.log('不及格');
  83. }
  84. }
  85. show3();
  86. //数组循环
  87. function show4()
  88. {
  89. var arr = ['a','b','c','dd'];
  90. for(let i=0;i<arr.length;i++){
  91. console.log(arr[i]);
  92. }
  93. }
  94. show4();
  95. parseInt() 函数可解析一个字符串,并返回一个整数。
  96. var aa = '123abc';
  97. console.log(aa); //123abc
  98. console.log(parseInt(aa)); //123
  99. break continue
  100. var bb = 10;
  101. while(bb>0){
  102. bb--;
  103. if(bb == 7){
  104. break; //这里如果是continue
  105. }
  106. console.log(bb); // 9 8
  107. }
  108. var bb = 10;
  109. while(bb>0){
  110. bb--;
  111. if(bb == 7){
  112. continue;
  113. }
  114. console.log(bb); // 9 8 6 5 4 3 2 1 0
  115. }
  116. </script>

提交小案例

  1. <input type="text" name="" id="age">
  2. <button onclick="save()">提交</button>
  3. <script>
  4. function save(){
  5. var age = document.getElementById('age').value;
  6. age = parseInt(age); //字符串转换成数字
  7. if(isNaN(age)){ // isNaN() 函数用于检查其参数是否是非数字值。
  8. return alert('请输入数字');
  9. }
  10. if(age>100 || age<0){
  11. console.log('请输入正确的年龄');
  12. return false;
  13. }
  14. console.log(age);
  15. }
  16. </script>
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!