JavaScript的方法和技巧

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

JavaScript就这么回事1:基础知识1创建脚本块1:scriptlanguage=”JavaScript”2:JavaScriptcodegoeshere3:/script2隐藏脚本代码1:scriptlanguage=”JavaScript”2:!--3:document.write(“Hello”);4://--5:/script在不支持JavaScript的浏览器中将不执行相关代码3浏览器不支持的时候显示1:noscript2:Hellotothenon-JavaScriptbrowser.3:/noscript4链接外部脚本文件1:scriptlanguage=”JavaScript”src=/”filename.js”/script5注释脚本1://Thisisacomment2:document.write(“Hello”);//Thisisacomment3:/*4:Allofthis5:isacomment6:*/6输出到浏览器1:document.write(“strongHello/strong”);7定义变量1:varmyVariable=“somevalue”;8字符串相加1:varmyString=“String1”+“String2”;9字符串搜索1:scriptlanguage=”JavaScript”2:!--3:varmyVariable=“Hellothere”;4:vartherePlace=myVariable.search(“there”);5:document.write(therePlace);6://--7:/scriptsearch方法指明是否存在相应的匹配。如果找到一个匹配,search方法将返回一个整数值,指明这个匹配距离字符串开始的偏移位置。如果没有找到匹配,则返回-1。10字符串替换1:thisVar.replace(“Monday”,”Friday”);11格式化字串scripttype=text/javascriptvarmyVariable=Hellothere;document.write(myVariable.big()+br/);document.write(myVariable.blink()+br/);document.write(myVariable.bold()+br/);document.write(myVariable.fixed()+br/);document.write(myVariable.fontcolor(red)+br/);document.write(myVariable.fontsize(18pt)+br/);document.write(myVariable.italics()+br/);document.write(myVariable.small()+br/);document.write(myVariable.strike()+br/);document.write(myVariable.sub()+br/);document.write(myVariable.sup()+br/);document.write(myVariable.toLowerCase()+br/);document.write(myVariable.toUpperCase()+br/);varfirstString=MyString;varfinalString=firstString.bold().toLowerCase().fontcolor(red);/script:b黑体,i斜体,u下划线,tt打字机体。12创建数组1:scriptlanguage=”JavaScript”2:!--3:varmyArray=newArray(5);4:myArray[0]=“FirstEntry”;5:myArray[1]=“SecondEntry”;6:myArray[2]=“ThirdEntry”;7:myArray[3]=“FourthEntry”;8:myArray[4]=“FifthEntry”;9:varanotherArray=newArray(“FirstEntry”,”SecondEntry”,”ThirdEntry”,”FourthEntry”,”FifthEntry”);10://--11:/scriptscripttype=text/javascriptvarmyArray=newArray(5);myArray[0]=FirstEntry;myArray[1]=SecondEntry;myArray[2]=ThirdEntry;myArray[3]=FourthEntry;myArray[4]=FifthEntry;vararr,str=;for(arrinmyArray){str+=arr+:+myArray[arr]+\n;}alert(str.length);/script13数组排序(对象数组)scripttype=text/javascriptvarmyArray=newArray(5);myArray[0]=z;myArray[1]=c;myArray[2]=d;myArray[3]=a;myArray[4]=q;document.write(myArray.sort());/script14分割字符串scripttype=text/javascriptvarmyVariable='a,b,c,d';varstringsplit=myVariable.split(',');for(i=0;istringsplit.length;i++){document.write(stringsplit[i]+'br');}/scriptsplit方法的结果是一个字符串数组15弹出警告信息scripttype=text/javascriptwindow.alert(hello);/script16弹出确认框scripttype=text/javascriptvartruthBeTold=window.confirm(单击“确定”继续。单击“取消”停止。);if(truthBeTold){window.alert(欢迎访问我们的Web页!);}elsewindow.alert(再见啦!);/script提示消息框scripttype=text/javascriptvartheResponse=window.prompt(欢迎?,请在此输入您的姓名。);document.write(theResponse);/script17定义函数scripttype=text/javascriptfunctionmultiple(number1,number2){varresult=number1*number2;returnresult;}/script18调用JS函数1:ahref=”#”onClick=”functionName()”Linktext/a2:ahref=/”javascript:functionName()”Linktext/a19在页面加载完成后执行函数bodyonLoad='functionName();'Bodyofthepage/body20条件判断scripttype=text/javascriptvaruserChoice=window.confirm(ChooseOKorCancel);varresult=(userChoice==true)?OK:Cancel;document.write(result);/script21指定次数循环1:script2:!--3:varmyArray=newArray(3);4:myArray[0]=“Item0”;5:myArray[1]=“Item1”;6:myArray[2]=“Item2”;7:for(i=0;imyArray.length;i++){8:document.write(myArray[i]+“br/”);9:}10://--11:/script22设定将来执行scripttype=text/javascriptFunctionhello(){window.alert(Hello);}window.setTimeout(hello(),5000);/script23定时执行函数scripttype=text/javascriptfunctionhello(){window.alert('Hello');window.setTimeout('hello()',5000);}window.setTimeout('hello()',5000);/script24取消定时执行scripttype=text/javascriptfunctionhello(){window.alert('Hello');}varmyTimeout=window.setTimeout('hello()',5000);window.clearTimeout(myTimeout);/script25在页面卸载时候执行函数bodyonUnload='functionName();'Bodyofthepage/bodyJavaScript就这么回事2:浏览器输出26访问document对象scripttype=text/javascriptvarmyURL=document.URL;window.alert(myURL);/script27动态输出HTMLscripttype=text/javascriptdocument.write('pHere’ssomeinformationaboutthisdocument:/p');document.write('ul');document.write('liReferringDocument:'+document.referrer+'/li');document.write('liDomain:'+document.domain+'/li');document.write('liURL:'+document.URL+'/li');document.write('/ul');/script28输出换行document.writeln('stronga/strong');document.writeln('b');29输出日期scripttype=text/javascriptvarthisDate=newDate();document.write(thisDate.toString()+br);document.write(thisDate);/script30指定日期的时区scripttype=text/javascriptvarmyOffset=-2;varcurrentDate=newDate();varuserOffset=currentDate.getTimezoneOffset()/60;vartimeZoneDifference=userOffset-myOffset;currentDate.setHours(currentDate.getHours()+timeZoneDifference);document.write(ThetimeanddateinCentralEuropeis:+currentDate.toLocaleString());/script31设置日期输出格式scripttype=text/javascriptvarthi

1 / 23
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功