现在的位置: 首页 > web前端 > 正文

JavaScript 学习_1

2019年11月24日 web前端 ⁄ 共 5044字 ⁄ 字号 评论关闭

 

JavaScript 学习_1

 

    学习过程的代码集.

  1. <html>
  2. <head>
  3. <title>**zczhu,learn javaScript</title>
  4. <script language = "JavaScript">
  5.     document.write("Welcome to javascript learn");
  6.     count = 100;
  7.     document.write("<br>count = ",count);
  8.     mParseInt = "100pstr" ;
  9.     document.write("<br><br>mParseInt = ", mParseInt);
  10.     document.write("<br>mParseInt(10) = ", parseInt(mParseInt));
  11.     document.write("<br>mParseInt(8) = ", parseInt(mParseInt,8));
  12.     document.write("<br>mParseInt(16)= ", parseInt(mParseInt,16));
  13.     
  14.     mParseInt = "100.2pstr中";
  15.     document.write("<br><br>mParseFloat = ", mParseInt);
  16.     document.write("<br>mParseFloat(10) = ", parseFloat(mParseInt));
  17.     
  18.     document.write("<br><br>type is: ",typeof(x));
  19.     document.write("<br>type is: ",typeof(x)==undefined);
  20.     document.write("<br>type is: "+typeof(x));
  21.     
  22.     document.write("<br><br>variable count type is: "+typeof(count));
  23.     document.write("<br>variable mParseInt type is: "+typeof(mParseInt));
  24.     
  25.     var myint = 100;
  26.     var mystring = "68";
  27.     var mybool = true;
  28.     var myaddress = "号";
  29.     
  30.     document.write("<br><br> myint = ",myint,"      mystring = ", mystring);
  31.     
  32.     result1 = myint + mystring;
  33.     document.write("<br><br> myint + mystring = ", result1);
  34.     
  35.     result2 = myint + parseInt(mystring);
  36.     document.write("<br><br>myint + parseInt(mystring) = ", result2);
  37.     document.write("<br> mybool = ", mybool);
  38.         
  39.     result3 = myint + mybool;
  40.     document.write("<br><br> myint + mybool = ", result3);
  41.     result4 = mystring + mybool;
  42.     document.write("<br><br> mystring + mybool = ", result4);
  43.     document.write("<br>myaddress = ", myaddress);
  44.     
  45.     result5 = myint + myint + myaddress;
  46.     document.write("<br><br>myint + myint + myaddress = ", result5);
  47.     
  48.     result6 = "" + myint + myint + myaddress;
  49.     document.write("<br><br> + myint + myint + myaddress = ", result6);
  50.     
  51.     var x;
  52.     x = prompt("please input x value:""");
  53.     if(x>0)
  54.     {
  55.             document.write("<br><br>x是正数");
  56.     }
  57.     else
  58.     {
  59.             document.write("<br><br>x是负数或者为0"); 
  60.     }
  61.     x = parseInt(x);
  62.     if(x>0)
  63.     {
  64.          document.write("<br><br>x的绝对值是:",x);
  65.     }
  66.     else
  67.     {
  68.          document.write("<br><br>x的绝对值是:",-x);
  69.     }
  70.     setBgColor();
  71.     function setBgColor()
  72.     {
  73.             with(document)
  74.             {
  75.                 bgColor = "pink";
  76.                 fgColor = "blue";
  77.                 write("背景是粉红色,前景(文字)是蓝色的");
  78.             }
  79.     }
  80.     
  81.     var iarr = new Array(1,2,3);
  82.     document.write("<br>iarr array is :", iarr);
  83.     document.write("<br>eval(5 == 6)",eval(5 == 6));
  84.     
  85.     document.write("*************Function define and call*****************");
  86.     function funcA()
  87.     {
  88.             var stringA = "This is function funcA define variable";
  89.             funcB();
  90.             function funcB()
  91.             {
  92.                 var stringB = "This is function funcB define variable";
  93.                 document.write("<br>function B variable: ", stringB);
  94.                 document.write("<br>function A variable in funcB call", stringA);
  95.             }
  96.     }
  97.     document.write("<br>Call funcA follow:");
  98.     funcA();
  99.     document.write("<br>Call funcA over");
  100.     
  101.     document.write("*************<br>Function define and call 递归函数*****************");
  102.     function f(n)
  103.     {
  104.         document.write("调用发f(",n,")<br>");
  105.         if(n==1)
  106.         {
  107.                 return 1;
  108.         }
  109.         else
  110.         {
  111.                 return f(n-1)+n;
  112.         }
  113.     }
  114.     document.write("f(10)的值为:", f(10));
  115.     
  116.     document.write("<br><br>*************Extended Regular Expressions*****************");
  117.     document.write("<br>*************'var varname = /pattern/flags' *****************");
  118.     document.write("<br>*************flag: I->忽略大小写,即进行字符串匹配时候。g->全局匹配。 m->进行多行匹配****");
  119.     
  120.     var language = new RegExp("JavaScript""ig");
  121.     var name = new RegExp("zzc");
  122.     var Date = new RegExp("1984");
  123.     
  124.     var mymatch = new Array();
  125.     var str = "JavaScript is powerful and javascript is very easy!";
  126.     var regxp = /javascript/ig;
  127.     mymatch = str.match(regxp);
  128.     
  129.     document.write("<br><br>最近一次匹配的字符串为:" + RegExp.input + "<br>");
  130.     document.write("匹配的字符串的个数为:" + mymatch.length + "<br>");
  131.     for(i = 0; i<mymatch.length; i++)
  132.     {
  133.         document.write("第"+i+"个匹配字符串为:" + mymatch[i]+ "<br>");
  134.     }
  135.     
  136.     document.write("<br>最近一次搜索的开始位置为:" + RegExp.index + "<br>");
  137.     document.write("下次搜索的开始位置为:" + RegExp.lastIndex + "<br>");
  138.     document.write("最近一次匹配的字符串前面的字串为:" + RegExp.leftContext + "<br>");
  139.     document.write("最近一次匹配的字符串后面的字串为:" + RegExp.rightContext + "<br>");
  140.     
  141.     document.write("<br><br>*************Replace()*****************<br>");
  142.     document.write("替换前的字符串为:" + str + "<br>");
  143.     var afterstr=str.replace(regxp, "JAVASCRIPT");
  144.     document.write("替换后的字符串为:" + afterstr + "<br>");
  145.     
  146.     document.write("<br><br>*************search()*****************<br>");
  147.     str = "JavaScript基础与实践教程";
  148.     regxp = /实践/;
  149.     var iindex = str.search(regxp); 
  150.     document.write("在字符串/'" + str +"/'的第" + iindex + "字符位置找到匹配模式" + regxp);
  151.     
  152.     document.write("<br><br>学习《JavaScript程序设计基础》此书到91页");   
  153. </script>
  154. </head>
  155. <html>

抱歉!评论已关闭.