js基本操作(基础快速入门篇)

来源:互联网 发布:个人出书知乎 编辑:程序博客网 时间:2024/05/20 02:28

js操作浏览器窗口window (2015/3/29 23:05:26)

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>无标题文档</title>
  6. <script>
  7. varcount10;
  8. functionupdate(){//一般在title中定义函数,在后面使用
  9. if(count>0)
  10. status=count--;
  11. }
  12. functioncloseW(){
  13. w.close();//w是全局变量,html文件内都可以访问
  14. }
  15. </script>
  16. </head>
  17. <body onload="setInterval('closeW()',1000);"><!--//body页面装载是调用onload-->
  18. <script>
  19. if(confirm("继续?")){
  20. alert(继续);
  21. } else{
  22. alert("Bye");
  23. }
  24. varname=prompt("your name:");
  25. alert(name);
  26. w=window.open("smallwin.html","smallwin","width=400,height=300,status=12125,resizable=yes");//后面的关于窗口属性参数可以不需要
  27. w.moveTo(0,0);//w.location()可以得到窗口的网址
  28. </script>
  29. <p onMouseOver="status='wangyi';" onmouseout="status='out';"> 段落</p>
  30. </body>
  31. </html>


js操作document元素 (2015/3/29 23:04:36)

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>无标题文档</title>
  6. </head>
  7. <body onload="setInterval('show()',200)">
  8. <img name="mama" height="200" src="1.png"/>
  9. <script>
  10. //document.mama.src="2.png";
  11. varimages=new Array(2);
  12. varindex=1;
  13. for(var i=0;i<2;i++){
  14. images[i]=new Image();//document中的元素也是对象
  15. images[i].src=i+".png";
  16. }
  17. functionshow(){
  18. document.mama.src=images[index].src;
  19. index=(index+1)%2;
  20. }
  21. </script>
  22. </body>
  23. </html>


js 操作document (2015/3/29 23:03:45)

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>无标题文档</title>
  6. </head>
  7. <body>
  8. <img name="mama" src="mama.jpg" />
  9. <p id="p1" name="dancing">段落</p>
  10. <script>
  11. alert(document.images[0].src +""+document.mama);
  12. alert(document.getElememntByName("dancing"));
  13. </script>
  14. <script>
  15. for(x indocument.mama)
  16. {
  17. document.write(x+"<br/>");
  18. }
  19. alert(document.mama.height);
  20. </script>
  21. document中的成员
  22. anchors[]
  23. forms[]
  24. images[]
  25. cookie
  26. title
  27. bgColor
  28. fgColor
  29. linkColor
  30. alinkColor
  31. vLinkColor
  32. </body>
  33. </html>


js函数 (2015/3/29 23:02:54)

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>无标题文档</title>
  6. </head>
  7. <body>
  8. <script>
  9. functionRect(w,h){//用来创建对象
  10. this.width=w;this.height=h;//this代表当前对象
  11. this.area=function(){
  12. returnthis.width*this.height;
  13. }
  14. }
  15. varr=new Rect(2,3);//新建对象
  16. alert(r.area());//调用对象内部的方法
  17. //prototype原型属性这个自己搜索了解
  18. </script>
  19. </body>
  20. </html>


js基础 (2015/3/29 23:01:40)

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>无标题文档</title>
  6. </head>
  7. <body>
  8. <script>
  9. document.write("<h1>helloworld</h1>");//document代表html文件对象
  10. varhello;//定义变量
  11. hello="hello";//变量初始化
  12. varage=20;//Javascript变量没有类型,但是值却可以有类型
  13. varot=true;
  14. document.write(hello+"<br/>");
  15. document.write(hello+age+"="+12/2+"<br/>");//+可以连接两个字符串或者变量
  16. document.write(age>12+"<br/>");
  17. document.write(hello>"eello"+"<br/>")//依次比较字符串中的各个字母
  18. //判断
  19. if(age>18){
  20. alert("成年了");
  21. }
  22. vart=1;
  23. switch(t){
  24. case1:
  25. alert("你好");break;
  26. case2:
  27. alert("good");break;
  28. }
  29. //循环
  30. while(t<3){
  31. alert(t);
  32. t++;
  33. }
  34. //定义函数
  35. varu=42;
  36. varv=24;
  37. functionfun_test(a,b){//可见定义函数时候并不需指点参数的类型
  38. returna+b;
  39. }
  40. document.write("u+v="+fun_test(u,v)+"<br/>");
  41. //函数变量
  42. varf=new Function("x","y","return x*y");//等价于function f(x,y){returnx*y};这样可以动态定义函数
  43. functionprintln(s)
  44. {
  45. document.write(s+"<br/>");
  46. }
  47. functionadd(a,b)
  48. {
  49. returna+b;
  50. }
  51. functioncal(f,a,b){
  52. returnf(a,b);//这里f是一个函数变量
  53. }
  54. document.write(cal(add,5,6)+"<br/>");//将函数名传入另个函数,将其作为参数
  55. //变量的作用空间:定义在函数外的变量在整个html文件中都有效;而定义在函数中的变量只在函数内有效
  56. //数组,javascript的数组是可以自动增加的
  57. varcolors=new Array();//或者var marks=new Array(size);var marks=newArray(d1,d2..);var d=[1,2,3,6];
  58. colors=["red","black","grean"];
  59. println("colors: "+colors[0]+""+colors[1]+""+colors.length);
  60. colors.length=2;//将数组长度缩小可以删除数组中的某些元素
  61. println("colors[2]: "+colors[2]);
  62. println(colors.toString());
  63. println(colors.join("||"));
  64. colors.push("gray","blue");//数组 做 堆栈
  65. colors.pop();
  66. //排序
  67. colors.sort();//排序
  68. colors.sort(compare);//调用函数进行排序
  69. functioncompare(value1,value2){
  70. if(value1<value2){
  71. return1;
  72. }
  73. elseif(value1<value2){
  74. return-1;
  75. }else{
  76. return0;
  77. }
  78. }
  79. colors.reverse();
  80. varcolors2=colors.contact("yello",["black","brown:"])//数组连接
  81. varcolors3=colors.slice(1,2);//取出数组当中的元素 1 到3
  82. colors.splice(0,2);//删除从0开始,删除两个
  83. //splice(开始位置,删除个数,插入元素);
  84. colors.splice(1,0,"red","green");//删除0个,在1处插入两个
  85. //对象
  86. varo =new Object();//创建对象 varciclr={x:0,y:0,radius:2} varciclr={x:0,y:0,radius:2};//这里使用大括号而不是方括号,数组才用方括号
  87. o.name="luoshengfeng";
  88. o.age=20;
  89. o.salary=10000;
  90. for(var x in o)//遍历对象中的所有属性for(var inObject)
  91. {
  92. println(x);//访问得到对象的属性名称
  93. println(o[x]);//得到对象属性的值
  94. }
  95. </script>
  96. </body>
  97. </html>



来自为知笔记(Wiz)


0 0