基础语法结构和数据类型

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <titile>JS</titile>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. alert(1+1);
  10. console.log(1+1);
  11. // 注释
  12. /* 多行注释
  13. var可 重复赋值
  14. let不可重复赋值
  15. const 常量
  16. */
  17. var a=1;
  18. let b=1;
  19. const c=3;
  20. /*
  21. 数值类型 int/float
  22. 字符串
  23. 数组 ==> go slice
  24. 字典(object) ==> go map
  25. */
  26. var height=1.68;
  27. const gender= 'gale'
  28. let age=31;
  29. let isMale=false;
  30. var nullVal=null; //nil
  31. console.log(typeof(name),typeof(gender),typeof(age),typeof(height),typeof(isMale));
  32. //运算
  33. //四则运算 +-*/ %
  34. //位运算 & | ^ ~ << >>
  35. //赋值运算 =,+=,-=,*=,/=,%=
  36. //布尔运算 && || !
  37. //关系运算 > < >= <= != == ===
  38. //数值运算:
  39. //字符串 只能使用 + 运算符
  40. //数组,js是弱类型语言
  41. var name = ["kk","小凡","xq",1];
  42. console.log(typeof(names),names);
  43. //数组四种操作方式 shift,unshift,push ,pop;
  44. /*
  45. shift 往后移动一位
  46. unshift(value) 在前面添加value
  47. push(value) 在后面添加value
  48. pop向前面移动一位
  49. */
  50. var names=[]
  51. names.push(1)
  52. names.push(2)
  53. names.push(3)
  54. console.log(names)
  55. names.shift()
  56. console.log(names)
  57. names.unshift(2)
  58. console.log(names)
  59. names.pop()
  60. console.log(names)
  61. //连接数组
  62. names.concat(name)
  63. if(true){
  64. console.log(names);
  65. } else if (1==1){
  66. console.log(names);
  67. } else {
  68. console.log();
  69. }
  70. A=1;
  71. switch (A) {
  72. case 0:console.log("0");break; //没有break会继续往下执行,与go不同点
  73. case 1:console.log("1");break;
  74. case 2:console.log("2");break;
  75. default:console.log("default");
  76. }
  77. // 字典
  78. var user={a:1,b:2}
  79. console.log(user)
  80. //函数
  81. function add(a,b){
  82. return a+b; //弱类型语言,不需要设置返回值类型
  83. }
  84. var rt =add(1,2);
  85. console.log(rt);
  86. //循环
  87. for(var key in user){
  88. console.log(key,user[key]);
  89. if(true){
  90. continue;
  91. // or
  92. break;
  93. }
  94. }
  95. do{
  96. console.log(i);
  97. i++;
  98. } while(i<10);
  99. </script>
  100. </body>
  101. </html>

dom文档

js是用来修改dom文档的,修改他的内容、样式、属性等等等

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <titile>DOM</titile>
  6. </head>
  7. <body>
  8. <div id="mydiv"></div>
  9. <script type="text/javascript">
  10. var div=document.getElementById("mydiv");
  11. div.innerHTML=new Date(); //修改div的内容
  12. console.log(div);
  13. setTimeout(function(){ //延时执行,只执行一次
  14. console.log("timeout",new Date());
  15. },10*1000);
  16. setInterval(function(){
  17. console.log("interval",new Date());
  18. div.innerHTML= new Date();
  19. },5*1000); //每间隔5秒执行一次
  20. confirm("确认删除吗?")
  21. </script>
  22. </body>
  23. </html>

js也遵循先加载,再使用的原则,如下使用jquery库(先从jquery.com下载库)

文档更新时间: 2023-11-30 16:20   作者:张尚