<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>JavaScript页面</title> <script language="javascript"> var myName = "isaac"; document.write(myName); </script> </head> <body> <p>正文内容</p> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>slice()和substring()</title> <script language="javascript"> var sMyString = "Tsinghua University"; document.write(sMyString.slice(1,3) + "<br>"); document.write(sMyString.substring(1,3) + "<br>"); document.write(sMyString.slice(4) + "<br>"); document.write(sMyString + "<br>"); //不改变原字符串 </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>slice()和substring()的区别</title> <script language="javascript"> var sMyString = "Tsinghua University"; document.write(sMyString.slice(2,-3) + "<br>"); document.write(sMyString.substring(2,-3) + "<br>"); document.write(sMyString.substring(2,0) + "<br>"); document.write(sMyString + "<br>"); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>indexOf()和lastIndexOf()</title> <script language="javascript"> var sMyString = "Tsinghua University"; document.write(sMyString.indexOf("i")+"<br>"); //从前往后 document.write(sMyString.indexOf("i",3)+"<br>"); //可选参数,从第几个字符开始往后找 document.write(sMyString.lastIndexOf("i")+"<br>"); //从后往前 document.write(sMyString.lastIndexOf("i",3)+"<br>"); //可选参数,从第几个字符开始往前找 document.write(sMyString.lastIndexOf("V")+"<br>"); //大写“V”找不到,返回-1 </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>数值计算</title> <script language="javascript"> var mynum1 = 23.345; var mynum2 = 45; var mynum3 = -34; var mynum4 = 9e5; //科学计数法 alert(mynum1 + " " + mynum2 + " " + mynum3 + " " + mynum4); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>数值计算</title> <script language="javascript"> var fNumber = 895.4; alert(fNumber.toExponential(1)); alert(fNumber.toExponential(2)); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>布尔型</title> <script language="javascript"> var married = true; alert("1." + typeof(married)); married = "true"; alert("2." + typeof(married)); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>类型转换</title> <script language="javascript"> var a = 3; var b = a + ""; var c = a.toString(); var d = "student" + a; alert(typeof(a) + " " + typeof(b) + " " + typeof(c) + " " + typeof(d)); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>toString()方法</title> <script language="javascript"> var a=11; document.write(a.toString(2) + "<br>"); document.write(a.toString(3) + "<br>"); document.write(a.toString(8) + "<br>"); document.write(a.toString(16) + "<br>"); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>parseInt()方法</title> <script language="javascript"> document.write(parseInt("4567red") + "<br>"); document.write(parseInt("53.5") + "<br>"); document.write(parseInt("0xC") + "<br>"); //直接进制转换 document.write(parseInt("[email protected]") + "<br>"); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>parseInt()方法</title> <script language="javascript"> document.write(parseInt("AF",16) + "<br>"); document.write(parseInt("11",2) + "<br>"); document.write(parseInt("011") + "<br>"); //0开头,默认为八进制 document.write(parseInt("011",8) + "<br>"); document.write(parseInt("011",10) + "<br>"); //指定为十进制 </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>parseFloat()方法</title> <script language="javascript"> document.write(parseFloat("34535orange") + "<br>"); document.write(parseFloat("0xA") + "<br>"); //不再有默认进制,直接输出第一个字符“0” document.write(parseFloat("435.34") + "<br>"); document.write(parseFloat("435.34.564") + "<br>"); document.write(parseFloat("[email protected]") + "<br>"); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>数组</title> <script language="javascript"> var aMap = new Array("China","USA","Britain"); alert(aMap.length + " " + aMap[2]); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>数组</title> <script language="javascript"> var aMap = new Array("China","USA","Britain"); aMap[20] = "Korea"; alert(aMap.length + " " + aMap[10] + " " + aMap[20]); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>数组</title> <script language="javascript"> var aMap = ["China","USA","Britain"]; aMap[20] = "Korea"; alert(aMap.length + " " + aMap[10] + " " + aMap[20]); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>数组</title> <script language="javascript"> var aMap = ["China","USA","Britain"]; alert(aMap.toString() + " " + typeof(aMap.toString())); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>join()方法</title> <script language="javascript"> var aMap = ["China","USA","Britain"]; document.write(aMap.join() + "<br>"); //无参数,等同于toString() document.write(aMap.join("") + "<br>"); //不用连接符 document.write(aMap.join("][") + "<br>"); //用“][”来连接 document.write(aMap.join("-isaac-") + "<br>"); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>split()方法</title> <script language="javascript"> var sFruit = "apple,pear,peach,orange"; var aFruit = sFruit.split(","); alert(aFruit.join("--")); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>reverse()方法</title> <script language="javascript"> var aFruit = ["apple","pear","peach","orange"]; alert(aFruit.reverse().toString()); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>字符串的reverse()</title> <script language="javascript"> var sMyString = "abcdefg"; alert(sMyString.split("").reverse().join("")); /* split("")将每一个字符转为一个数组元素 reverse()反序数组的每个元素 join("")再最后将数组无连接符的转为字符串 */ </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>sort()方法</title> <script language="javascript"> var aFruit = ["pear","apple","peach","orange"]; aFruit.sort(); alert(aFruit.toString()); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>ջ</title> <script language="javascript"> var stack = new Array(); stack.push("red"); stack.push("green"); stack.push("blue"); document.write(stack.toString() + "<br>"); var vItem = stack.pop(); document.write(vItem + "<br>"); document.write(stack.toString()); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <body> <script language="javascript"> document.write("Pear" == "Pear"); document.write("<br>"); document.write("Apple" < "Orange"); document.write("<br>"); document.write("apple" < "Orange"); </script> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <body> <script language="javascript"> document.write(3>2 && 4>3); document.write("<br>"); document.write(3>2 && 4<3); document.write("<br>"); document.write(4<3 || 3>2); document.write("<br>"); document.write(!(3>2)); </script> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>if语句</title> </head> <body> <script language="javascript"> //首先获取用户的一个输入,并用Number()强制转换为数字 var iNumber = Number(prompt("输入一个5到100之间的数字", "")); if(isNaN(iNumber)) //判断输入的是否是数字 document.write("请确认你的输入正确"); else if(iNumber > 100 || iNumber < 5) //判断输入的数字范围 document.write("你输入的数字范围不在5和100之间"); else document.write("你输入的数字是:" + iNumber); </script> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>switch语句</title> </head> <body> <script language="javascript"> iWeek = parseInt(prompt("输入1到7之间的整数","")); switch(iWeek){ case 1: document.write("Monday"); break; case 2: document.write("Tuesday"); break; case 3: document.write("Wednesday"); break; case 4: document.write("Thursday"); break; case 5: document.write("Friday"); break; case 6: document.write("Saturday"); break; case 7: document.write("Sunday"); break; default: document.write("Error"); } </script> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>while语句</title> </head> <body> <script language="javascript"> var i=iSum=0; while(i<=100){ iSum += i; i++; } alert(iSum); </script> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>do..while语句</title> </head> <body> <script language="javascript"> var aNumbers = new Array(); var sMessage = "你输入了:\n"; var iTotal = 0; var vUserInput; var iArrayIndex = 0; do{ vUserInput = prompt("输入一个数字,或者'0'退出","0"); aNumbers[iArrayIndex] = vUserInput; iArrayIndex++; iTotal += Number(vUserInput); sMessage += vUserInput + "\n"; }while(vUserInput != 0) //当输入为0(默认值)时退出循环体 sMessage += "总数:" + iTotal; alert(sMessage); </script> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>for语句</title> </head> <body> <script language="javascript"> var aNumbers = new Array(); var sMessage = "你输入了:\n"; var iTotal = 0; var vUserInput; var iArrayIndex = 0; do{ vUserInput = prompt("输入一个数字,或者'0'退出","0"); aNumbers[iArrayIndex] = vUserInput; iArrayIndex++; }while(vUserInput != 0) //当输入为0(默认值)时退出循环体 //for循环遍历数组的常用方法: for(var i=0;i<aNumbers.length;i++){ iTotal += Number(aNumbers[i]); sMessage += aNumbers[i] + "\n"; } sMessage += "总数:" + iTotal; alert(sMessage); </script> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>break语句</title> </head> <body> <script language="javascript"> var aNumbers = new Array(); var sMessage = "你输入了:<br>"; var iTotal = 0; var vUserInput; var iArrayIndex = 0; do{ vUserInput = Number(prompt("输入一个数字,或者'0'退出","0")); if(isNaN(vUserInput)){ document.write("输入错误,请输入数字,'0'退出<br>"); break; //输入错误直接退出整个do循环体 } aNumbers[iArrayIndex] = vUserInput; iArrayIndex++; }while(vUserInput != 0) //当输入为0(默认值)时退出循环体 //for循环遍历数组的常用方法: for(var i=0;i<aNumbers.length;i++){ iTotal += Number(aNumbers[i]); sMessage += aNumbers[i] + "<br>"; } sMessage += "总数:" + iTotal; document.write(sMessage); </script> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>continue语句</title> </head> <body> <script language="javascript"> var aNumbers = new Array(); var sMessage = "你输入了:<br>"; var iTotal = 0; var vUserInput; var iArrayIndex = 0; do{ vUserInput = Number(prompt("输入一个数字,或者'0'退出","0")); if(isNaN(vUserInput)){ alert("输入错误,请输入数字,'0'退出"); continue; //输入错误则退出当前循环,继续下一次循环 } aNumbers[iArrayIndex] = vUserInput; iArrayIndex++; }while(vUserInput != 0) //当输入为0(默认值)时退出循环体 //for循环遍历数组的常用方法: for(var i=0;i<aNumbers.length;i++){ iTotal += Number(aNumbers[i]); sMessage += aNumbers[i] + "<br>"; } sMessage += "总数:" + iTotal; document.write(sMessage); </script> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>九九乘法表</title> </head> <body bgcolor="#e0f1ff"> <table cellpadding="6" cellspacing="0" style="border-collapse:collapse; border:none;"> <script language="javascript"> for(var i=1;i<10;i++){ //乘法表一共九行 document.write("<tr>"); //每行是table的一行 for(j=1;j<10;j++) //每行都有9个单元格 if(j<=i) //有内容的单元格 document.write("<td style='border:2px solid #004B8A; background:#FFFFFF;'>"+i+"*"+j+"="+(i*j)+"</td>"); else //没有内容的单元格 document.write("<td style='border:none;'></td>"); document.write("</tr>"); } </script> </table> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>arguments.length</title> <script language="javascript"> function ArgsNum(){ return arguments.length; } document.write(ArgsNum("isaac",25) + "<br>"); document.write(ArgsNum() + "<br>"); document.write(ArgsNum(3) + "<br>"); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>arguments</title> <script language="javascript"> function fnAdd(){ if(arguments.length == 0) return; else if(arguments.length == 1) return arguments[0] + 5; else{ var iSum = 0; for(var i=0;i<arguments.length;i++) iSum += arguments[i]; return iSum; } } document.write(fnAdd(45) + "<br>"); document.write(fnAdd(45,50) + "<br>"); document.write(fnAdd(45,50,55,60) + "<br>"); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>杨辉三角</title> <script language="javascript"> function Combination(m,n){ if(n==0) return 1; //每行第一个数为1 else if(m==n) return 1; //最后一个数为1 //其余都是上一行相邻元素相加而来 else return Combination(m-1,n-1)+Combination(m-1,n); } function Pascal(n){ //杨慧三角,n为行数 for(var i=0;i<n;i++){ //一共n行 for(var j=0;j<=i;j++) //每行数字的个数即为行号,例如第一行1个数,第二行2个数 document.write(Combination(i,j)+" "); document.write("<br>"); } } Pascal(10); //直接传入希望得到的杨辉三角的行数 </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Date对象</title> <script language="javascript"> var myDate1 = new Date(); //运行代码前的时间 for(var i=0;i<3000000;i++); var myDate2 = new Date(); //运行代码后的时间 alert(myDate2-myDate1); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Date对象</title> <script language="javascript"> var oMyDate = new Date(); var iYear = oMyDate.getFullYear(); var iMonth = oMyDate.getMonth() + 1; //月份是从0开始的 var iDate = oMyDate.getDate(); var iDay = oMyDate.getDay(); switch(iDay){ case 0: iDay = "星期日"; break; case 1: iDay = "星期一"; break; case 2: iDay = "星期二"; break; case 3: iDay = "星期三"; break; case 4: iDay = "星期四"; break; case 5: iDay = "星期五"; break; case 6: iDay = "星期六"; break; default: iDay = "error"; } document.write("今天是" + iYear + "年" + iMonth +"月" + iDate + "日," + iDay); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Date对象</title> <script language="javascript"> function disDate(oDate, iDate){ var ms = oDate.getTime(); //换成毫秒数 ms -= iDate*24*60*60*1000; //计算相差的毫秒数 return new Date(ms); //返回新的时间对象 } var oBeijing = new Date(2008,7,8); var iNum = 100; //前100天 var oMyDate = disDate(oBeijing, iNum); document.write(oMyDate.getFullYear()+"年"+(oMyDate.getMonth()+1)+"月"+oMyDate.getDate()+"日" + "距离"+oBeijing.getFullYear()+"年"+(oBeijing.getMonth()+1)+"月"+oBeijing.getDate()+"日为"+iNum+"天"); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Math.random()</title> <script language="javascript"> function selectFrom(iFirstValue, iLastValue){ var iChoices = iLastValue - iFirstValue + 1; //计算项数 return Math.floor(Math.random()*iChoices+iFirstValue); } var iNum = selectFrom(2,99); //随机选择数字 var aFruits = ["apple","pear","peach","orange","watermelon","banana"]; //随机选择数组元素 var sFruit = aFruits[selectFrom(0,aFruits.length-1)]; alert(iNum + " " + sFruit); </script> </head> <body> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>检测浏览器和操作系统</title> <script language="javascript"> var sUserAgent = navigator.userAgent; //检测Opera、KHTML var isOpera = sUserAgent.indexOf("Opera") > -1; var isKHTML = sUserAgent.indexOf("KHTML") > -1 || sUserAgent.indexOf("Konqueror") > -1 || sUserAgent.indexOf("AppleWebKit") > -1; //检测IE、Mozilla var isIE = sUserAgent.indexOf("compatible") > -1 && sUserAgent.indexOf("MSIE") > -1 && !isOpera; var isMoz = sUserAgent.indexOf("Gecko") > -1 && !isKHTML; //检测操作系统 var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows"); var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh"); var isUnix = (navigator.platform == "X11") && !isWin && !isMac; if(isOpera) document.write("Opera "); if(isKHTML) document.write("KHTML "); if(isIE) document.write("IE "); if(isMoz) document.write("Mozilla "); if(isWin) document.write("Windows"); if(isMac) document.write("Mac"); if(isUnix) document.write("Unix"); </script> </head> <body> </body> </html>