菜鸟学习javascript实例教程用JS显示文字的例子:htmlbodyscripttype=text/javascriptdocument.write(HelloWorld!)/script/body/html用HTML标签来格式化文本的例子:htmlbodyscripttype=text/javascriptdocument.write(h1HelloWorld!/h1)/script/body/html书写JS位置的例子:打开页面弹出窗口的例子htmlheadscripttype=text/javascriptfunctionmessage(){alert(网页教学网欢迎你的光临)}/script/headbodyonload=message()/body/html在BODY区内输出显示文本的例子:htmlhead/headbodyscripttype=text/javascriptdocument.write(欢迎光临网页教学网)/script/body/html调用其它的一个JS文件的例子:htmlhead/headbodyscriptsrc=xxx.js/scriptpTheactualscriptisinanexternalscriptfilecalledxxx.js./p/body/html变量的使用变量使用的例子:htmlbodyscripttype=text/javascriptvarname=Hegedocument.write(name)document.write(h1name/h1)/scriptpThisexampledeclaresavariable,assignsavaluetoit,andthendisplaysthevariable./ppThenthevariableisdisplayedonemoretime,onlythistimeasaheading./p/body/html函数的例子函数使用的一个例子:htmlheadscripttype=text/javascriptfunctionmyfunction(){alert(HELLO)}/script/headbodyforminputtype=buttononclick=myfunction()value=Callfunction/formpBypressingthebutton,afunctionwillbecalled.Thefunctionwillalertamessage./p/body/html带一个参数的函数的例子:htmlheadscripttype=text/javascriptfunctionmyfunction(txt){alert(txt)}/script/headbodyforminputtype=buttononclick=myfunction('Hello')value=Callfunction/formpBypressingthebutton,afunctionwithanargumentwillbecalled.Thefunctionwillalertthisargument./p/body/html不同的两个参数调用函数的例子:htmlheadscripttype=text/javascriptfunctionmyfunction(txt){alert(txt)}/script/headbodyforminputtype=buttononclick=myfunction('GoodMorning!')value=IntheMorninginputtype=buttononclick=myfunction('GoodEvening!')value=IntheEvening/formpWhenyouclickononeofthebuttons,afunctionwillbecalled.Thefunctionwillalerttheargumentthatispassedtoit./p/body/html利用函数返回值的例子:htmlheadscripttype=text/javascriptfunctionmyFunction(){return(Hello,haveaniceday!)}/script/headbodyscripttype=text/javascriptdocument.write(myFunction())/scriptpThescriptinthebodysectioncallsafunction./ppThefunctionreturnsatext./p/body/html带参数的函数返回值的例子:htmlheadscripttype=text/javascriptfunctiontotal(numberA,numberB){returnnumberAnumberB}/script/headbodyscripttype=text/javascriptdocument.write(total(2,3))/scriptpThescriptinthebodysectioncallsafunctionwithtwoarguments,2and3./ppThefunctionreturnsthesumofthesetwoarguments./p/body/html条件语句的例子简单条件语句的例子:htmlbodyscripttype=text/javascriptvard=newDate()vartime=d.getHours()if(time10){document.write(bGoodmorning/b)}/scriptpThisexampledemonstratestheIfstatement./ppIfthetimeonyourbrowserislessthan10,youwillgetaGoodmorninggreeting./p/body/htmlif...else的条件语句的例子:htmlbodyscripttype=text/javascriptvard=newDate()vartime=d.getHours()if(time10){document.write(bGoodmorning/b)}else{document.write(bGoodday/b)}/scriptpThisexampledemonstratestheIf...Elsestatement./ppIfthetimeonyourbrowserislessthan10,youwillgetaGoodmorninggreeting.OtherwiseyouwillgetaGooddaygreeting./p/body/html用随机数产生连接的例子:htmlbodyscripttype=text/javascriptvarr=Math.random()if(r0.5){document.write(ahref='!/a)}else{document.write(ahref='!/a)}/script/body/html多条件的语句实现的例子:htmlbodyscripttype=text/javascriptvard=newDate()theDay=d.getDay()switch(theDay){case5:document.write(FinallyFriday)breakcase6:document.write(SuperSaturday)breakcase0:document.write(SleepySunday)breakdefault:document.write(I'mreallylookingforwardtothisweekend!)}/scriptpThisexampledemonstratestheswitchstatement./ppYouwillreceiveadifferentgreetingbasedonwhatdayitis./ppNotethatSunday=0,Monday=1,Tuesday=2,etc./p/body/html循环的例子:for循环的一个例子:htmlbodyscripttype=text/javascriptfor(i=0;i=5;i){document.write(Thenumberisi)document.write(br)}/scriptpExplanation:/ppTheforloopsetsbi/bequalto0./ppAslongasbi/bislessthan,orequalto,5,theloopwillcontinuetorun./ppbi/bwillincreaseby1eachtimetheloopruns./p/body/html复杂循环的一个例子:htmlbodyscripttype=text/javascriptfor(i=1;i=6;i){document.write(hiThisisheaderi)document.write(/hi)}/script/body/html按条件循环while的例子:htmlbodyscripttype=text/javascripti=0while(i=5){document.write(Thenumberisi)document.write(br)i}/scriptpExplanation:/ppbi/bequalto0./ppWhilebi/bislessthan,orequalto,5,theloopwillcontinuetorun./ppbi/bwillincreaseby1eachtimetheloopruns./p/body/htmldo...while循环的例子:htmlbodyscripttype=text/javascripti=0do{document.write(Thenumberisi)document.write(br)i}while(i=5)/scriptpExplanation:/ppbi/bequalto0./ppTheloopwillrun/ppbi/bwillincreaseby1eachtimetheloopruns./ppWhilebi/bislessthan,orequalto,5,theloopwillcontinuetorun./p/body/html字符串对象的例子:检测字符串长度的例子:htmlbodyscripttype=tex