您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > JavaScript练习题
1JavaScript练习题一.函数部分(1)编写一个函数,在页面上输出1~1000之间所有能同时被3,5,7整除的证书,并要求每行显示6个这样的数。ex050303.htmhtmlheadscriptlanguage=javascript!--functionIsThatNumber(x){returnx%3==0&&x%5==0&&x%7==0;}//--/script/headbodyprescriptlanguage=javascript!--varn,nb=0;for(n=1;n1000;n++){if(IsThatNumber(n)){if(nb%60)document.write(,);nb++;document.write(n);if(nb%6==0)document.write(\n);}}document.write(\n\n);document.write(共有+nb+个数);//--/script/pre/body/html(2)利用全局变量和函数,设计模拟幸运数字机游戏。设幸运数字为8,每次由计算机2随机生成3个1~9之间的随机数,当这3个随机数中有一个数字为8时,就算赢了一次。Ex050309.htmlHTMLHEADMETAname=GENERATORcontent=MicrosoftFrontPage5.0METAname=ProgIdcontent=FrontPage.Editor.DocumentSCRIPTlanguage=javascriptvarwin_rate=0;//赢率varplay_times=0;//总次数varwins=0;//赢的次数varlast_digits;//上次数字串varlast_win;//上次是否赢?!--functionPlayOnce(){//模拟玩一次数字机游戏vari,digit;play_times++;last_digits=;last_win=false;for(i=0;i3;i++){digit=Math.floor(Math.random()*9)+1;last_digits+=digit;if(digit==8)last_win=true;}if(last_win)wins++;win_rate=Math.floor(100*(wins/play_times));}//--/SCRIPT/HEADBODYPRESCRIPTlanguage=javascript!--while(true){3PlayOnce();if(!confirm(last_digits+\n+(last_win?赢:输)+\n胜率+win_rate+%,继续吗?))break;}//--/SCRIPT/PRE/BODY/HTML二.事件与对象1.设计一个表单,放入两个按钮,单击它们时将显示不同问候语。S07_02.HTMHTMLHEADTITLE处理事件-HTML标记方式/TITLEscriptlanguage=javascriptfunctionhello_girl(){alert(小姐,您好!);}/script/HEADBODYFORMname=form1INPUTtype=buttonvalue=问侯先生name=hello1onclick=alert('先生,您好!');/PINPUTtype=buttonvalue=问侯小姐name=hello2onclick=returnhello_girl();/P/FORM/BODY/HTML三.内置对象1.在页面中显示当天日期。4S06_03.HTM:HTMLHEADTITLE使用new运算符/TITLE/HEADBODYscriptlanguage=javascriptvartoday;today=newDate();document.write(今天是+today.getFullYear()+年+(today.getMonth()+1)+月+today.getDate()+日);/script/BODY/HTML2.在浏览器窗口的状态栏中显示当前浏览器的版本信息。S06_02.HTMHTMLHEADTITLE访问对象的属性/TITLEscriptlanguage=javascriptwindow.status=navigator.appVersion;/script/HEADBODY/BODY/HTML3.将用户输入的字符串反向输出到页面上,并且要求将其中的小写字母转换成大写字母。S06_07.HTMHTMLHEADTITLE使用String/TITLE/HEADBODYscriptlanguage=javascriptvarorigin_s,upper_s,i;origin_s=prompt(请输入一行文字:,);upper_s=origin_s.toUpperCase();for(i=upper_s.length-1;i=0;i--)document.write(upper_s.charAt(i));/script/BODY/HTML4.求PI的5次方,并四舍五入取整。5S06_04.HTMHTMLHEADTITLE使用Math对象/TITLEscriptlanguage=javascriptalert(Math.round(Math.pow(Math.PI,5)));/script/HEADBODY/BODY/HTML5.由图像表示日期。ch3_14.htmscriptlanguage=JavaScript!--varsWeek=newArray(日,一,二,三,四,五,六);varmyDate=newDate();//当天的日期varsYear=myDate.getFullYear();//年varsMonth=myDate.getMonth()+1;//月varsDate=myDate.getDate();//日varsDay=sWeek[myDate.getDay()];//星期document.write(imageDigits(sYear)+ +imageDigits(sMonth)+ +imageDigits(sDate)+br);//如果输入数是1位数,在十位数上补0functionformatTwoDigits(s){if(s10)return0+s;elsereturns;}//将数转换为图像,注意,在本文件的相同目录下已有0-9的图像文件,文件名为0.gif,1.gif……以此类推functionimageDigits(s){varret=;vars=newString(s);for(vari=0;is.length;i++){ret+='imgsrc='+s.charAt(i)+'.gif';}returnret;}//--/script6四.DOM部分1.设计一个含有一个表单的页面,并且在表单上放入一个文本框。编写程序,当鼠标在页面上移动时,鼠标的坐标将显示在这个文本框中。ex070303.htmHTMLHEADSCRIPTid=clientEventHandlersJSlanguage=javascript!--functiondocument_onmousemove(){form1.txtMousePosition.value=window.event.clientX+,+window.event.clientY;}//--/SCRIPTSCRIPTlanguage=javascriptfor=documentevent=onmousemove!--returndocument_onmousemove()//--/SCRIPT/HEADBODYFORMid=form1PINPUTtype=textname=txtMousePositionsize=20/P/FORM/BODY/HTML2.在窗体中有两个多选列表,用户可以从左侧列表中选择任意项,添加到右侧列表中。反之亦然。ch4_07.htmhtmlheadscriptlanguage=JavaScript!--//moveList用于对两个多选列表进行选项的移动操作7//from为需要移动的列表名称,to为移动到列表名称functionmoveList(from,to){varfromList=document.myForm.elements[from];varfromLen=fromList.options.length;vartoList=document.myForm.elements[to];vartoLen=toList.options.length;//current为需要移动列表中的当前选项序号varcurrent=fromList.selectedIndex;//如果需要移动列表中有选择项,则进行移动操作while(current-1){//o为需要移动列表中当前选择项对象varo=fromList.options[current];vart=o.text;varv=o.value;//根据已选项新建一个列表选项varoptionName=newOption(t,v,false,false);//将该选项添加到移动到列表中toList.options[toLen]=optionName;toLen++;//将该选项从需要移动列表中清除fromList.options[current]=null;current=fromList.selectedIndex;}}//--/script/headbodyformname=myFormtabletrvalign=toptdselectname=leftListmultiplesize=6style=width:50px;optiona/optionoptionb/optionoptionc/option/select/tdtd!--通过事件onclick调用JavaScript的moveList函数--inputtype=buttonname=tovalue=8onclick=moveList('leftList','rightList')pinputtype=buttonname=backTovalue=onclick=moveList('rightList','leftList')p/tdtdselectname=rightListmultiplesize=6style=width:50px;optiond/optionoptione/optionoptionf/option/select/td/tr/table/form/body/html3.设计一个有3个超链接的页面,单击这些链接时分别打开和关闭窗口以及关闭本身窗口。S08_01.HTMHTMLHEADTITLE打开和关闭窗口/TITLESCRIPTlanguage=javascriptvarnewwin;functionopennewwin(){newwin=open(=100,width=400,top=10,left=0,toolbar
本文标题:JavaScript练习题
链接地址:https://www.777doc.com/doc-3899084 .html