<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JavaScript基礎應用</title> </head> <body> <h3>javascript的使用方法</h3> <hr/> <script type="text/javascript"> alert("第一種使用方法"); </script> <a href="javascript:alert(new Date())">第二種使用方法查看時間</a> <input type="button" value="第三種使用方法點擊" onclick="alert(new Date())"> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JavaScript基礎應用</title> <script src="javascri2/js1.js" language="javascript" type="text/javascript" charset=「gbk」> </script> </head> <body> <script type="text/javascript"> </script> </body> </html>
js1.js
// JavaScript Document alert("第四種使用方法");
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>變量的使用</title> </head> <body> <script type="text/javascript"> var inum=100; var sName="張三"; var fnum=10.0; alert(inum); alert(typeof sName); alert(fnum.toString()); alert(parseInt("123a")); alert(Boolean(0)); alert(1+1); </script> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>控制語句</title> </head> <body> <script type="text/javascript"> var inum1=2; var inum2=3; if(inum1>inum2){ alert("inum1>inum2"); }else{ alert("inum1<inum2"); } var inum=9; switch(inum){ case 1: alert(1); case 3: alert(3); case 5: alert(5); case 7: alert(7); case 9: alert(9); case 10: alert(10); default: alert(11);break; } </script> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>函數</title> </head> <body> <script type="text/javascript"> var s=test2(3,4); alert(s); function test1(){ return "沒有參數"; } function test2(a,b){ return a+b; } function test3(a,b){ return a-b; } function test4(){ for(var i=0;i<arguments.length;i++){ alert(arguments[i]); } } test4(10,20,30); </script> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> </head> <body> <script> var a="This is a string"; alert(a.length); alert(a.charAt(2)); alert(a.indexOf(2)); alert(a.lastIndexOf(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 xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>日期類</title> </head> <body> <script language="javascript"> var currTime=new Date(); var str=currTime.getYear()+"年"; alert(str); alert(currTime.toLocaleString()); var uri1="http://www.tjitcast.com/index.html?country=中國&name=zs"; alert(encodeURI(uri1)); var uri2="http://www.tjitcast.com/index.html?country=%E4%B8%AD%E5%9B%BD&name=zs"; alert(decodeURI(uri1)); var uri3="http://www.tjitcast.com/index.html?country=中國&name=zs"; alert(encodeURIComponent(uri3)); alert(parseInt("avd",16)); alert(parseFloat("abc")); alert(isNaN(parseFloat("abc"))); </script> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>建立本身的類和對象 </title> </head> <body> <script> function Animal(){ } var animal=new Animal; animal.name="dog"; animal.age=2; function xiaohou(){ alert(animal.name+"--->"+animal["age"]); } animal.say=xiaohou; animal.say(); function Person(){ this.name="張三"; this.age=20; this.height=176; } var p=new Person(); var temp,str=""; for(temp in p){ str+=p[temp]+" "; } alert(str); var arr=new Array(2); arr[0]=0; arr[1]=1; arr[2]=2; arr[3]=3; alert(arr[3]); //3 alert(arr.toString()); //0123 alert(arr.join("--")); //0-1-2-3 alert(arr.reverse()); //3210 alert(arr.sort()); //0123 alert(arr.pop()); //3 alert(arr.toString()); //012 alert(arr.push([1],[2],[3])); //6 alert(arr.shift()); //0 alert(arr.unshift([2],[3],[6])); //8 </script> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JS事件 HTML事件</title> <style type="text/css"> .input-text{ border-right: #a1bca3 1px solid; border-top: #a1bca3 1px solid; border-leB: #a1bca3 1px solid; border-boJom: #a1bca3 1px solid; font-size: 11px; height: 20px; width: 160px; } function executClick(){ alert("事件按鈕"); } </style> </head> <body> <input type="text" name="txt" class="input-text" onfocus="this.style.borderColor='red'" onblur="this.style.borderColor='#A1BCA3'"/> <hr/><hr/> <input type="button" value="點擊" onclick="executClick()"/> <img src="../btn1.png" onmouseout="this.src='../btn2.png'" onmouseover="this.src='../btn1.png'" /> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JS事件</title> <script type="text/javascript"> function executClick(){ alert("事件按鈕"); } </script> </head> <body> <input type="button" value="點擊" onclick="executClick()"/> <input type="text" name="txt" onkeydown="alert(event.keyCode)"/> </body> </html>