Blogger Information
Blog 37
fans 0
comment 0
visits 20805
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
前端第十一课:javascript基础-PHP培训九期线上班
渡劫小能手
Original
371 people have browsed it

1、javascript变量、函数的定义

  1. <script type="text/javascript">
  2. //var变量声明
  3. var a=11;
  4. //输出变量
  5. console.log(a);
  6. alert('22');
  7. //函数定义
  8. function fun(){
  9. console.log('33');
  10. }
  11. //函数调用
  12. fun();
  13. </script>


2、javascript流程控制if else switch

  1. <script type="text/javascript">
  2. //流程控制 if
  3. function f1(){
  4. var int=100;
  5. if (int>100 || int<0) {
  6. alert('非法值!!!');
  7. }else if (int>90) {
  8. alert('优秀!');
  9. }else if (int<=90 && int>70) {
  10. alert('及格');
  11. }else{
  12. alert('没及格,挨打');
  13. }
  14. }
  15. //流程控制 switch
  16. function f2() {
  17. var int=99;
  18. switch(true){
  19. case(int >100 || int < 0):
  20. alert('非法值!');
  21. break;
  22. case(int>=90):
  23. alert('优秀了!');
  24. break;
  25. case(int>=70):
  26. alert('及格了!');
  27. break;
  28. default:
  29. alert('挨打了!');
  30. }
  31. }
  32. //流程控制 if优化
  33. function f3(){
  34. var int=80;
  35. if (int >100 || int < 0) {
  36. alert('非法值');
  37. }
  38. if (int>=90 && int <= 100 ) {
  39. alert('优秀');
  40. }
  41. if (int<=90 && int>=70) {
  42. alert('及格');
  43. }
  44. if (int<70) {
  45. alert('没过')
  46. }
  47. }
  48. f3();
  49. </script>

3、javascript三种循环

  1. <script type="text/javascript">
  2. //循环 for
  3. function x1(){
  4. for (var i = 1; i <=10; i++) {
  5. console.log(i);
  6. }
  7. }
  8. //循环 while
  9. function x2() {
  10. var i=10;
  11. while(i<=100){
  12. console.log(i);
  13. i+=10;
  14. }
  15. }
  16. //循环 do while
  17. function x3() {
  18. var i=2;
  19. do{
  20. console.log(i);
  21. i+=2;
  22. }while (i<=20);
  23. }
  24. x3();
  25. </script>

4、数据类型转换:parseInt、isNaN函数的使用

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <input type="text" name="" id="age" placeholder="输入您的年龄">
  9. <button onclick="age();">提交</button>
  10. <script type="text/javascript">
  11. function age() {
  12. var age=document.getElementById('age').value;
  13. //通过parseInt()函数把age强制转换成整数
  14. age = parseInt(age);
  15. //isNaN判断值是否为NaN
  16. if (isNaN(age)) {
  17. return alert('年龄值不合法!');
  18. }
  19. if (age>120 || age<0) {
  20. return alert('年龄超过可提交范围,请核实');
  21. }
  22. return alert('您的年龄为:'+age);
  23. }
  24. </script>
  25. </body>
  26. </html>

5、总结

1.NaN 属性是代表非数字值的特殊值,该属性用于指示某个值不是数字。
2.parseInt() 函数可解析一个字符串,并返回一个整数。
3.typeof()函数可以判断变量类型。
4.undefined未定义的变量的数据类型。

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!