JavaScript 学习_1
学习过程的代码集.
- <html>
- <head>
- <title>**zczhu,learn javaScript</title>
- <script language = "JavaScript">
- document.write("Welcome to javascript learn");
- count = 100;
- document.write("<br>count = ",count);
- mParseInt = "100pstr" ;
- document.write("<br><br>mParseInt = ", mParseInt);
- document.write("<br>mParseInt(10) = ", parseInt(mParseInt));
- document.write("<br>mParseInt(8) = ", parseInt(mParseInt,8));
- document.write("<br>mParseInt(16)= ", parseInt(mParseInt,16));
- mParseInt = "100.2pstr中";
- document.write("<br><br>mParseFloat = ", mParseInt);
- document.write("<br>mParseFloat(10) = ", parseFloat(mParseInt));
- document.write("<br><br>type is: ",typeof(x));
- document.write("<br>type is: ",typeof(x)==undefined);
- document.write("<br>type is: "+typeof(x));
- document.write("<br><br>variable count type is: "+typeof(count));
- document.write("<br>variable mParseInt type is: "+typeof(mParseInt));
- var myint = 100;
- var mystring = "68";
- var mybool = true;
- var myaddress = "号";
- document.write("<br><br> myint = ",myint," mystring = ", mystring);
- result1 = myint + mystring;
- document.write("<br><br> myint + mystring = ", result1);
- result2 = myint + parseInt(mystring);
- document.write("<br><br>myint + parseInt(mystring) = ", result2);
- document.write("<br> mybool = ", mybool);
- result3 = myint + mybool;
- document.write("<br><br> myint + mybool = ", result3);
- result4 = mystring + mybool;
- document.write("<br><br> mystring + mybool = ", result4);
- document.write("<br>myaddress = ", myaddress);
- result5 = myint + myint + myaddress;
- document.write("<br><br>myint + myint + myaddress = ", result5);
- result6 = "" + myint + myint + myaddress;
- document.write("<br><br> + myint + myint + myaddress = ", result6);
- var x;
- x = prompt("please input x value:", "");
- if(x>0)
- {
- document.write("<br><br>x是正数");
- }
- else
- {
- document.write("<br><br>x是负数或者为0");
- }
- x = parseInt(x);
- if(x>0)
- {
- document.write("<br><br>x的绝对值是:",x);
- }
- else
- {
- document.write("<br><br>x的绝对值是:",-x);
- }
- setBgColor();
- function setBgColor()
- {
- with(document)
- {
- bgColor = "pink";
- fgColor = "blue";
- write("背景是粉红色,前景(文字)是蓝色的");
- }
- }
- var iarr = new Array(1,2,3);
- document.write("<br>iarr array is :", iarr);
- document.write("<br>eval(5 == 6)",eval(5 == 6));
- document.write("*************Function define and call*****************");
- function funcA()
- {
- var stringA = "This is function funcA define variable";
- funcB();
- function funcB()
- {
- var stringB = "This is function funcB define variable";
- document.write("<br>function B variable: ", stringB);
- document.write("<br>function A variable in funcB call", stringA);
- }
- }
- document.write("<br>Call funcA follow:");
- funcA();
- document.write("<br>Call funcA over");
- document.write("*************<br>Function define and call 递归函数*****************");
- function f(n)
- {
- document.write("调用发f(",n,")<br>");
- if(n==1)
- {
- return 1;
- }
- else
- {
- return f(n-1)+n;
- }
- }
- document.write("f(10)的值为:", f(10));
- document.write("<br><br>*************Extended Regular Expressions*****************");
- document.write("<br>*************'var varname = /pattern/flags' *****************");
- document.write("<br>*************flag: I->忽略大小写,即进行字符串匹配时候。g->全局匹配。 m->进行多行匹配****");
- var language = new RegExp("JavaScript", "ig");
- var name = new RegExp("zzc");
- var Date = new RegExp("1984");
- var mymatch = new Array();
- var str = "JavaScript is powerful and javascript is very easy!";
- var regxp = /javascript/ig;
- mymatch = str.match(regxp);
- document.write("<br><br>最近一次匹配的字符串为:" + RegExp.input + "<br>");
- document.write("匹配的字符串的个数为:" + mymatch.length + "<br>");
- for(i = 0; i<mymatch.length; i++)
- {
- document.write("第"+i+"个匹配字符串为:" + mymatch[i]+ "<br>");
- }
- document.write("<br>最近一次搜索的开始位置为:" + RegExp.index + "<br>");
- document.write("下次搜索的开始位置为:" + RegExp.lastIndex + "<br>");
- document.write("最近一次匹配的字符串前面的字串为:" + RegExp.leftContext + "<br>");
- document.write("最近一次匹配的字符串后面的字串为:" + RegExp.rightContext + "<br>");
- document.write("<br><br>*************Replace()*****************<br>");
- document.write("替换前的字符串为:" + str + "<br>");
- var afterstr=str.replace(regxp, "JAVASCRIPT");
- document.write("替换后的字符串为:" + afterstr + "<br>");
- document.write("<br><br>*************search()*****************<br>");
- str = "JavaScript基础与实践教程";
- regxp = /实践/;
- var iindex = str.search(regxp);
- document.write("在字符串/'" + str +"/'的第" + iindex + "字符位置找到匹配模式" + regxp);
- document.write("<br><br>学习《JavaScript程序设计基础》此书到91页");
- </script>
- </head>
- <html>