JavaScript使用方法和技巧大全

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

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

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

资源描述

有些时候你精通一门语言,但是会发现你其实整天在和其它语言打交道,也许你以为这些微不足道,不至于影响你的开发进度,但恰恰是这些你不重视的东西会浪费你很多时间,我一直以为我早在几年前就已经精通JavaScript了,直到目前,我才越来越觉得JavaScript远比我想象的复杂和强大,我开始崇拜它,就像崇拜所有OOP语言一样~趁着节日的空隙,把有关JavaScript的方法和技巧整理下,让每个在为JavaScript而烦恼的人明白,JavaScript就这么回事!并希望JavaScript还可以成为你的朋友,让你豁然开朗,在项目中更好的应用~适合阅读范围:对JavaScript一无所知~离精通只差一步之遥的人基础知识:HTMLJavaScript就这么回事:基础知识1创建脚本块1:scriptlanguage=JavaScript2:JavaScriptcodegoeshere3:/script2隐藏脚本代码1:scriptlanguage=JavaScript2:!--3:document.write(Hello);4://--5:/script在不支持JavaScript的浏览器中将不执行相关代码3浏览器不支持的时候显示1:noscript2:Hellotothenon-JavaScriptbrowser.3:/noscript4链接外部脚本文件1:scriptlanguage=JavaScriptsrc=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=JavaScript2:!--3:varmyVariable=Hellothere;4:vartherePlace=myVariable.search(there);5:document.write(therePlace);6://--7:/script10字符串替换1:thisVar.replace(Monday,Friday);11格式化字串1:scriptlanguage=JavaScript2:!--3:varmyVariable='Hellothere;4:document.write(myVariable.big()+br);5:document.write(myVariable.blink()+br);6:document.write(myVariable.bold()+br);7:document.write(myVariable.fixed()+br);8:document.write(myVariable.fontcolor(red)+br);9:document.write(myVariable.fontsize(18pt)+br);10:document.write(myVariable.italics()+br);11:document.write(myVariable.small()+br);12:document.write(myVariable.strike()+br);13:document.write(myVariable.sub()+br);14:document.write(myVariable.sup()+br);15:document.write(myVariable.toLowerCase()+br);16:document.write(myVariable.toUpperCase()+br);17:18:varfirstString=MyString;19:varfinalString=firstString.bold().toLowerCase().fontcolor(red);20://--21:/script12创建数组1:scriptlanguage=JavaScript2:!--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:/script13数组排序1:scriptlanguage=JavaScript2:!--3:varmyArray=newArray(5);4:myArray[0]=z;5:myArray[1]=c;6:myArray[2]=d;7:myArray[3]=a;8:myArray[4]=q;9:document.write(myArray.sort());10://--11:/script14分割字符串1:scriptlanguage=JavaScript2:!--3:varmyVariable=a,b,c,d;4:varstringArray=myVariable.split(,);5:document.write(stringArray[0]);6:document.write(stringArray[1]);7:document.write(stringArray[2]);8:document.write(stringArray[3]);9://--10:/script15弹出警告信息1:scriptlanguage=JavaScript2:!--3:window.alert(Hello);4://--5:/script16弹出确认框1:scriptlanguage=JavaScript2:!--3:varresult=window.confirm(ClickOKtocontinue);4://--5:/script17定义函数1:scriptlanguage=JavaScript2:!--3:functionmultiple(number1,number2){4:varresult=number1*number2;5:returnresult;6:}7://--8:/script18调用JS函数1:ahref=#onClick=functionName()Linktext/a2:ahref=()Linktext/a19在页面加载完成后执行函数1:bodyonLoad=functionName();2:Bodyofthepage3:/body20条件判断1:script2:!--3:varuserChoice=window.confirm(ChooseOKorCancel);4:varresult=(userChoice==true)?OK:Cancel;5:document.write(result);6://--7:/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设定将来执行1:script2:!--3:functionhello(){4:window.alert(Hello);5:}6:window.setTimeout(hello(),5000);7://--8:/script23定时执行函数1:script2:!--3:functionhello(){4:window.alert(Hello);5:window.setTimeout(hello(),5000);6:}7:window.setTimeout(hello(),5000);8://--9:/script24取消定时执行1:script2:!--3:functionhello(){4:window.alert(Hello);5:}6:varmyTimeout=window.setTimeout(hello(),5000);7:window.clearTimeout(myTimeout);8://--9:/script25在页面卸载时候执行函数1:bodyonUnload=functionName();2:Bodyofthepage3:/bodyJavaScript就这么回事2:浏览器输出26访问document对象1:scriptlanguage=JavaScript2:varmyURL=document.URL;3:window.alert(myURL);4:/script27动态输出HTML1:scriptlanguage=JavaScript2:document.write(pHere’ssomeinformationaboutthisdocument:/p);3:document.write(ul);4:document.write(liReferringDocument:+document.referrer+/li);5:document.write(liDomain:+document.domain+/li);6:document.write(liURL:+document.URL+/li);7:document.write(/ul);8:/script28输出换行1:document.writeln(stronga/strong);2:document.writeln(b);29输出日期1:scriptlanguage=JavaScript2:varthisDate=newDate();3:document.write(thisDate.toString());4:/script30指定日期的时区1:scriptlanguage=JavaScript2:varmyOffset=-2;3:varcurrentDate=newDate();4:varuserOffset=currentDate.getTimezoneOffset()/60;5:vartimeZoneDifference=userOffset-myOffset;6:currentDate.setHours(currentDate.getHours()+timeZoneDifference);7:document.write(ThetimeanddateinCentralEuropeis:+currentDate.toLocaleString());8:/script31设置日期输出格式1:scriptlanguage=JavaScript2:varthisDate=newDate();3:varthisTimeString=thisDate.getHours()+:+thisDate.ge

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

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

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

×
保存成功