Blogger Information
Blog 34
fans 0
comment 0
visits 22316
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
12月16日—Javascript第一天
曾龙宇
Original
610 people have browsed it

定义变量和方法

  1. <script>
  2. //定义变量
  3. var num1 = 20;
  4. var num2 = "20";
  5. alert(typeof num1);
  6. alert(num1);
  7. alert(typeof(num2));
  8. alert(num2);
  9. //定义方法
  10. function show(){
  11. alert('我叫张三');
  12. }
  13. //调用方法
  14. show();
  15. //定义带参数方法
  16. function callname(name){
  17. alert('我叫'+name);
  18. }
  19. //调用方法时要传参
  20. callname('李四');
  21. </script>

条件判断

  1. <script>
  2. //if-else判断
  3. var data = 100;
  4. if (data > 100){
  5. console.log('data的值:'+data);
  6. }else{
  7. console.log('data值太小,不输出');
  8. }
  9. //if-else if-else判断
  10. var price = 4;
  11. var money = 10;
  12. if(money<price){
  13. console.log('你的钱不够');
  14. }else if (money==5){
  15. console.log('应该找零'+(money-price)+'元');
  16. }else if(money==10){
  17. console.log('应该找零'+(money-price)+'元');
  18. }else{
  19. console.log('太大,找不开');
  20. }
  21. //switch-case判断
  22. var score = 91;
  23. switch (parseInt(score/10)) {
  24. case 10:
  25. console.log('满分,厉害啊');
  26. break;
  27. case 9:
  28. console.log('差一点就满分了,更努力点');
  29. break;
  30. case 8:
  31. case 7:
  32. console.log('不要气馁,还要继续努力');
  33. break;
  34. case 6:
  35. console.log('要加倍努力才行啊');
  36. break;
  37. default:
  38. console.log('放学不要走,留堂');
  39. }
  40. </script>



循环

  1. <script>
  2. //for循环
  3. for (var i=0;i<10;i++){
  4. console.log('i的值是:'+i);
  5. }
  6. //while循环
  7. var data = 10;
  8. while(data>0){
  9. data--;
  10. console.log('i的值是'+data);
  11. }
  12. //do-while循环
  13. var flag = 10;
  14. do{
  15. flag--;
  16. console.log('flag:'+flag);
  17. // console.log('不符合条件,都会执行一次');
  18. }while(flag>0);
  19. </script>




数据类型转换

  1. <body>
  2. <input type="text" name="age" id="age" placeholder="请输入年龄" value="">
  3. <button onclick="change()">转换</button>
  4. <script>
  5. function change() {
  6. var age = document.getElementById('age').value;
  7. var ageInt = parseInt(age);
  8. if (ageInt>100 || ageInt<0){
  9. console.log('输入年龄区间错误');
  10. }else if (isNaN(ageInt)){
  11. console.log('转换失败');
  12. }else{
  13. console.log('年龄是:'+ageInt);
  14. }
  15. }
  16. </script>
  17. </body>

总结

①、javascript使用关键字“var”来定义变量,typeof可以获取变量的数据类型
②、输出方式有alert(‘内容’),弹框输出,会中断程序执行过程;
console.log(‘内容’),控制台输出,在浏览器中按F12,进入开发者工具,在Console页面可以看到输出。
③、条件判断有if-else if-else,switch-case
④、循环有for、while、do-while
⑤、html界面input标签获取的任何值都是”string”类型的,可以使用parseInt()转换成整数,parseFloat()转换成浮点数,isNaN()用于检查其参数是否是非数字值。

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!