您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 人事档案/员工关系 > 菜鸟javascript实例
菜鸟javascript实例.txt始终相信,这世间,相爱的原因有很多,但分开的理由只有一个--爱的还不够。人生有四个存折:健康情感事业和金钱。如果健康消失了,其他的存折都会过期。用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(h1+name+/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){returnnumberA+numberB}/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(Thenumberis+i)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(h+i+Thisisheader+i)document.write(/h+i+)}/script/body/html按条件循环while的例子:htmlbodyscripttype=text/javascripti=0while(i=5){document.write(Thenumberis+i)document.write(br)i++}/scriptpExplanation:/ppbi/bequalto0./ppWhilebi/bislessthan,orequalto,5,theloopwillcontinuetorun./ppbi/bwillincreaseby1eachtimetheloopruns./p/body/htmldo...while循环的例子:htmlbodyscripttype=text/javascripti=0do{document.write(Thenumberis+i)document.write(br)i++}while(i=5)/scriptpExplanation:/ppbi/bequalto0./ppTheloopwillrun/ppbi/bwillincreaseby1eachtimetheloopruns./ppWhilebi/bislessthan,orequalto,5
本文标题:菜鸟javascript实例
链接地址:https://www.777doc.com/doc-6103110 .html