JavaScript常用代码总结

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

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

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

资源描述

JavaScript常用代码总结1创建脚本块scriptlanguage=JavaScriptJavaScriptcodegoeshere/script2隐藏脚本代码scriptlanguage=JavaScript!--document.write(Hello);//--/script在不支持JavaScript的浏览器中将不执行相关代码3浏览器不支持的时候显示noscriptHellotothenon-JavaScriptbrowser./noscript4链接外部脚本文件scriptlanguage=JavaScriptsrc=filename.js/script5注释脚本//Thisisacommentdocument.write(Hello);//Thisisacomment/*Allofthisisacomment*/6输出到浏览器document.write(strongHello/strong);7定义变量varmyVariable=somevalue;8字符串相加varmyString=String1+String2;9字符串搜索scriptlanguage=JavaScript!--varmyVariable=Hellothere;vartherePlace=myVariable.search(there);document.write(therePlace);//--/script10字符串替换thisVar.replace(Monday,Friday);11格式化字串scriptlanguage=JavaScript!--varmyVariable='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);17:varfirstString=MyString;varfinalString=firstString.bold().toLowerCase().fontcolor(red);//--2/script12创建数组scriptlanguage=JavaScript!--varmyArray=newArray(5);myArray[0]=FirstEntry;myArray[1]=SecondEntry;myArray[2]=ThirdEntry;myArray[3]=FourthEntry;myArray[4]=FifthEntry;varanotherArray=newArray(FirstEntry,SecondEntry,ThirdEntry,FourthEntry,FifthEntry);//--1/script13数组排序scriptlanguage=JavaScript!--varmyArray=newArray(5);myArray[0]=z;myArray[1]=c;myArray[2]=d;myArray[3]=a;myArray[4]=q;document.write(myArray.sort());//--1/script14分割字符串scriptlanguage=JavaScript!--varmyVariable=a,b,c,d;varstringArray=myVariable.split(,);document.write(stringArray[0]);document.write(stringArray[1]);document.write(stringArray[2]);document.write(stringArray[3]);//--/script15弹出警告信息scriptlanguage=JavaScript!--window.alert(Hello);//--/script16弹出确认框scriptlanguage=JavaScript!--varresult=window.confirm(ClickOKtocontinue);//--/script17定义函数scriptlanguage=JavaScript!--functionmultiple(number1,number2){varresult=number1*number2;returnresult;}//--/script18调用JS函数ahref=#onClick=functionName()Linktext/aahref=()Linktext/a19在页面加载完成后执行函数bodyonLoad=functionName();Bodyofthepage/body20条件判断script!--varuserChoice=window.confirm(ChooseOKorCancel);varresult=(userChoice==true)?OK:Cancel;document.write(result);//--/script21指定次数循环script!--varmyArray=newArray(3);myArray[0]=Item0;myArray[1]=Item1;myArray[2]=Item2;for(i=0;imyArray.length;i++){document.write(myArray[i]+br);}//--/script22设定将来执行script!--functionhello(){window.alert(Hello);}window.setTimeout(hello(),5000);//--/script23定时执行函数script!--functionhello(){window.alert(Hello);window.setTimeout(hello(),5000);}window.setTimeout(hello(),5000);//--/script24取消定时执行script!--functionhello(){window.alert(Hello);}varmyTimeout=window.setTimeout(hello(),5000);window.clearTimeout(myTimeout);//--/script25在页面卸载时候执行函数bodyonUnload=functionName();Bodyofthepage/bodyJavaScript就这么回事2:浏览器输出26访问document对象scriptlanguage=JavaScriptvarmyURL=document.URL;window.alert(myURL);/script27动态输出HTMLscriptlanguage=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输出日期scriptlanguage=JavaScriptvarthisDate=newDate();document.write(thisDate.toString());/script30指定日期的时区scriptlanguage=JavaScriptvarmyOffset=-2;varcurrentDate=newDate();varuserOffset=currentDate.getTimezoneOffset()/60;vartimeZoneDifference=userOffset-myOffset;currentDate.setHours(currentDate.getHours()+timeZoneDifference);document.write(ThetimeanddateinCentralEuropeis:+currentDate.toLocaleString());/script31设置日期输出格式scriptlanguage=JavaScriptvarthisDate=newDate();varthisTimeString=thisDate.getHours()+:+thisDate.getMinutes();varthisDateString=thisDate.getFullYear()+/+thisDate.getMonth()+/+thisDate.getDate();document.write(thisTimeString+on+thisDateString);/script32读取URL参数scriptlanguage=JavaScriptvarurlParts=document.URL.split(?);varparameterParts=urlParts[1].split(&);for(i=0;iparameterParts.length;i++){varpairParts=parameterParts[i].split(=);varpairName=pairParts[0];varpairValue=pairParts[1];document.write(pairName+:+pairValue);}/script你还以为HTML是无状态的么?33打开一个新的document对象scriptlanguage=JavaScriptfunctionnewDocument(){document.open();document.write(pThisisaNewDocument./p);document.close();}/script34页面跳转scriptlanguage=JavaScriptwindow.location=添加网页加载进度

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

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

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

×
保存成功