《js中的小技巧》word版_第1頁
《js中的小技巧》word版_第2頁
《js中的小技巧》word版_第3頁
《js中的小技巧》word版_第4頁
《js中的小技巧》word版_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、.js中的小技巧每一項(xiàng)都是js中的小技巧,但十分的實(shí)用! 1.document.write();輸出語句 2.JS中的注釋為/ 3.傳統(tǒng)的HTML文檔順序是:document-html-(head,body) 4.一個(gè)瀏覽器窗口中的DOM順序是:window-(navigator,screen,history,location,document) 5.得到表單中元素的名稱和值:document.getElementById(表單中元素的ID號).name(或value) 6.一個(gè)小寫轉(zhuǎn)大寫的JS:document.getElementById(output).value=document.ge

2、tElementById(input).value.toUpperCase(); 7.JS中的值類型:String,Number,Boolean,Null,Object,Function 8.JS中的字符型轉(zhuǎn)換成數(shù)值型:parseInt(),parseFloat() 9.JS中的數(shù)字轉(zhuǎn)換成字符型:(+變量) 10.JS中的取字符串長度是:(length) 11.JS中的字符與字符相連接使用+號. 12.JS中的比較操作符有:=等于,!=不等于,=,.= 13.JS中聲明變量使用:var來進(jìn)行聲明 14.JS中的判斷語句結(jié)構(gòu):if(condition)else 15.JS中的循環(huán)結(jié)構(gòu):for(i

3、nitialexpression;condition;upadteexpression)insideloop 16.循環(huán)中止的命令是:break 17.JS中的函數(shù)定義:functionfunctionName(parameter,.)statements 18.當(dāng)文件中出現(xiàn)多個(gè)form表單時(shí).可以用document.forms0,document.forms1來代替. 19.窗口:打開窗口window.open(),關(guān)閉一個(gè)窗口:window.close(),窗口本身:self 20.狀態(tài)欄的設(shè)置:window.status=字符; 21.彈出提示信息:window.alert(字符); 2

4、2.彈出確認(rèn)框:window.confirm(); 23.彈出輸入提示框:mpt(); 24.指定當(dāng)前顯示鏈接的位置:window.location.href=URL 25.取出窗體中的所有表單的數(shù)量:document.forms.length 26.關(guān)閉文檔的輸出流:document.close(); 27.字符串追加連接符:+= 28.創(chuàng)建一個(gè)文檔元素:document.createElement(),document.createTextNode() 29.得到元素的方法:document.getElementById() 30.設(shè)置表單中所有文本型的成員的值為空:

5、varform=window.document.forms0 for(vari=0;iform.elements.length;i+) if(form.elements.type=text) form.elements.value=; 31.復(fù)選按鈕在JS中判斷是否選中:document.forms0.checkThis.checked(checked屬性代表為是否選中返回TRUE或FALSE) 32.單選按鈕組(單選按鈕的名稱必須相同):取單選按鈕組的長度document.forms0.groupName.length 33.單選按鈕組判斷是否被選中也是用checked. 34.下拉列表框的

6、值:document.forms0.selectName.optionsn.value(n有時(shí)用下拉列表框名稱加上.selectedIndex來確定被選中的值) 35.字符串的定義:varmyString=newString(Thisislightsword); 36.字符串轉(zhuǎn)成大寫:string.toUpperCase();字符串轉(zhuǎn)成小寫:string.toLowerCase(); 37.返回字符串2在字符串1中出現(xiàn)的位置:String1.indexOf(String2)!=-1則說明沒找到. 38.取字符串中指定位置的一個(gè)字符:StringA.charAt(9); 39.取出字符串中指定起

7、點(diǎn)和終點(diǎn)的子字符串:stringA.substring(2,6); 40.數(shù)學(xué)函數(shù):Math.PI(返回圓周率),Math.SQRT2(返回開方),Math.max(value1,value2)返回兩個(gè)數(shù)中的最在值,Math.pow(value1,10)返回value1的十次方,Math.round(value1)四舍五入函數(shù),Math.floor(Math.random()*(n+1)返回隨機(jī)數(shù) 41.定義日期型變量:vartoday=newDate(); 42.日期函數(shù)列表:dateObj.getTime()得到時(shí)間,dateObj.getYear()得到年份,dateObj.getFul

8、lYear()得到四位的年份,dateObj.getMonth()得到月份,dateObj.getDate()得到日,dateObj.getDay()得到日期幾,dateObj.getHours()得到小時(shí),dateObj.getMinutes()得到分,dateObj.getSeconds()得到秒,dateObj.setTime(value)設(shè)置時(shí)間,dateObj.setYear(val)設(shè)置年,dateObj.setMonth(val)設(shè)置月,dateObj.setDate(val)設(shè)置日,dateObj.setDay(val)設(shè)置星期幾,dateObj.setHours設(shè)置小時(shí),da

9、teObj.setMinutes(val)設(shè)置分,dateObj.setSeconds(val)設(shè)置秒注意:此日期時(shí)間從0開始計(jì) 43.FRAME的表示方式:window.framesn.ObjFuncVarName,framesframeName.ObjFuncVarName,frameName.ObjFuncVarName 44.parent代表父親對象,top代表最頂端對象 45.打開子窗口的父窗口為:opener 46.表示當(dāng)前所屬的位置:this 47.當(dāng)在超鏈接中調(diào)用JS函數(shù)時(shí)用:(javascript:)來開頭后面加函數(shù)名 48.在老的瀏覽器中不執(zhí)行此JS: 49.引用一個(gè)文件式

10、的JS: 50.指定在不支持腳本的瀏覽器顯示的HTML: 51.當(dāng)超鏈和onCLICK事件都有時(shí),則老版本的瀏覽器轉(zhuǎn)向a.html,否則轉(zhuǎn)向b.html.例:dfsadf 52.JS的內(nèi)建對象有:Array,Boolean,Date,Error,EvalError,Function,Math,Number,Object,RangeError,ReferenceError,RegExp,String,SyntaxError,TypeError,URIError 53.JS中的換行:n 54.窗口全屏大小:functionfullScreen()this.moveTo(0,0);this.oute

11、rWidth=screen.availWidth;this.outerHeight=screen.availHeight;window.maximize=fullScreen; 55.JS中的all代表其下層的全部元素 56.JS中的焦點(diǎn)順序:document.getElementByid(表單元素).tabIndex=1 57.innerHTML的值是表單元素的值:如howareyou,則innerHTML的值就是:howareyou 58.innerTEXT的值和上面的一樣,只不過不會(huì)把這種標(biāo)記顯示出來. 59.contentEditable可設(shè)置元素是否可被修改,isContentEdi

12、table返回是否可修改的狀態(tài). 60.isDisabled判斷是否為禁止?fàn)顟B(tài).disabled設(shè)置禁止?fàn)顟B(tài) 61.length取得長度,返回整型數(shù)值 62.addBehavior()是一種JS調(diào)用的外部函數(shù)文件其擴(kuò)展名為.htc 63.window.focus()使當(dāng)前的窗口在所有窗口之前. 64.blur()指失去焦點(diǎn).與FOCUS()相反. 65.select()指元素為選中狀態(tài). 66.防止用戶對文本框中輸入文本:onfocus=this.blur() 67.取出該元素在頁面中出現(xiàn)的數(shù)量:document.all.tags(div(或其它HTML標(biāo)記符).length 68.JS中分為

13、兩種窗體輸出:模態(tài)和非模態(tài).window.showModaldialog(),window.showModeless() 69.狀態(tài)欄文字的設(shè)置:window.status=文字,默認(rèn)的狀態(tài)欄文字設(shè)置:window.defaultStatus=文字.; 70.添加到收藏夾:external.AddFavorite(;,jaskdlf); 71.JS中遇到腳本錯(cuò)誤時(shí)不做任何操作:window.onerror=doNothing;指定錯(cuò)誤句柄的語法為:window.onerror=handleError; 72.JS中指定當(dāng)前打開窗口的父窗口:window.

14、opener,支持opener.opener.的多重繼續(xù). 73.JS中的self指的是當(dāng)前的窗口 74.JS中狀態(tài)欄顯示內(nèi)容:window.status=內(nèi)容 75.JS中的top指的是框架集中最頂層的框架 76.JS中關(guān)閉當(dāng)前的窗口:window.close(); 77.JS中提出是否確認(rèn)的框:if(confirm(Areyousure?)alert(ok);elsealert(NotOk); 78.JS中的窗口重定向:window.navigate(;); 79.JS中的打印:window.print() 80.JS中的提示輸入框:window.

15、prompt(message,defaultReply); 81.JS中的窗口滾動(dòng)條:window.scroll(x,y) 82.JS中的窗口滾動(dòng)到位置:window.scrollby 83.JS中設(shè)置時(shí)間間隔:setInterval(expr,msecDelay)或setInterval(funcRef,msecDelay)或setTimeout 84.JS中的模態(tài)顯示在IE4+行,在NN中不行:showModalDialog(URL,arguments,features); 85.JS中的退出之前使用的句柄:functionverifyClose()event.returnValue=we

16、reallylikeyouandhopeyouwillstaylonger.;window.onbeforeunload=verifyClose; 86.當(dāng)窗體第一次調(diào)用時(shí)使用的文件句柄:onload() 87.當(dāng)窗體關(guān)閉時(shí)調(diào)用的文件句柄:onunload() 88.window.location的屬性:protocol(http:),hostname(),port(80),host(:80),pathname(/a/a.html),hash(#giantGizmo,指跳轉(zhuǎn)到相應(yīng)的錨記),href(全部的信息) 89.window.l

17、ocation.reload()刷新當(dāng)前頁面. 90.window.history.back()返回上一頁,window.history.forward()返回下一頁,window.history.go(返回第幾頁,也可以使用訪問過的URL) 91.document.write()不換行的輸出,document.writeln()換行輸出 92.document.body.noWrap=true;防止鏈接文字折行. 93.變量名.charAt(第幾位),取該變量的第幾位的字符. 94.abc.charCodeAt(第幾個(gè)),返回第幾個(gè)字符的ASCii碼值. 95.字符串連接:string.co

18、ncat(string2),或用+=進(jìn)行連接 96.變量.indexOf(字符,起始位置),返回第一個(gè)出現(xiàn)的位置(從0開始計(jì)算) 97.string.lastIndexOf(searchString,startIndex)最后一次出現(xiàn)的位置. 98.string.match(regExpression),判斷字符是否匹配. 99.string.replace(regExpression,replaceString)替換現(xiàn)有字符串. 100.string.split(分隔符)返回一個(gè)數(shù)組存儲(chǔ)值. 101.string.substr(start,length)取從第幾位到指定長度的字符串. 102

19、.string.toLowerCase()使字符串全部變?yōu)樾? 103.string.toUpperCase()使全部字符變?yōu)榇髮? 104.parseInt(string,radix(代表進(jìn)制)強(qiáng)制轉(zhuǎn)換成整型. 105.parseFloat(string,radix)強(qiáng)制轉(zhuǎn)換成浮點(diǎn)型. 106.isNaN(變量):測試是否為數(shù)值型. 107.定義常量的關(guān)鍵字:const,定義變量的關(guān)鍵字:var1. oncontextmenu=window.event.returnValue=false 將徹底屏蔽鼠標(biāo)右鍵 table border oncontextmenu=return(false)

20、tdno /table 可用于Table 2. body onselectstart=return false 取消選取、防止復(fù)制 3. onpaste=return false 不準(zhǔn)粘貼 4. oncopy=return false; oncut=return false; 防止復(fù)制 5. link rel=Shortcut Icon href=favicon.ico IE地址欄前換成自己的圖標(biāo) 6. link rel=Bookmark href=favicon.ico 可以在收藏夾中顯示出你的圖標(biāo) 7. input style=ime-mode:disabled 關(guān)閉輸入法 8. 永遠(yuǎn)都會(huì)

21、帶著框架 script language=JavaScript !- if (window = top)top.location.href = frames.htm; /frames.htm為框架網(wǎng)頁 / - /script 9. 防止被人frame SCRIPT LANGUAGE=JAVASCRIPT !- if (top.location != self.location)top.location=self.location; / - /SCRIPT 10. 網(wǎng)頁將不能被另存為 noscript iframe src=*.html /iframe /noscript 11. input t

22、ype=button value=查看網(wǎng)頁源代碼 onclick=window.location = view-source:+ 12.刪除時(shí)確認(rèn) a href=javascript:if(confirm(確實(shí)要?jiǎng)h除嗎?)location=boos.asp?&areyou=刪除&page=1刪除 /a 13. 取得控件的絕對位置 /Javascript script language=Javascript function getIE(e) var t=e.offsetTop; var l=e.offsetLeft; while(e=e.of

23、fsetParent) t+=e.offsetTop; l+=e.offsetLeft; alert(top=+t+/nleft=+l); /script /VBScript script language=VBScript !- function getIE() dim t,l,a,b set a=document.all.img1 t=document.all.img1.offsetTop l=document.all.img1.offsetLeft while a.tagName BODY set a = a.offsetParent t=t+a.offsetTop l=l+a.offs

24、etLeft wend msgbox top=&t&chr(13)&left=&l,64,得到控件的位置 end function - /script 14. 光標(biāo)是停在文本框文字的最后 script language=javascript function cc() var e = event.srcElement; var r =e.createTextRange(); r.moveStart(character,e.value.length); r.collapse(true); r.select(); /script input type=text name=text1 value=1

25、23 onfocus=cc() 15. 判斷上一頁的來源 javascript: document.referrer 16. 最小化、最大化、關(guān)閉窗口 object id=hh1 classid=clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11 param name=Command value=Minimize /object object id=hh2 classid=clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11 param name=Command value=Maximize /object OBJECT id=

26、hh3 classid=clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11 PARAM NAME=Command VALUE=Close /OBJECT input type=button value=最小化 onclick=hh1.Click() input type=button value=最大化 onclick=hh2.Click() input type=button value=關(guān)閉 onclick=hh3.Click() 本例適用于IE 17.屏蔽功能鍵Shift,Alt,Ctrl script function look() if(event.

27、shiftKey) alert(禁止按Shift鍵!); /可以換成ALTCTRL document.onkeydown=look; /script 18. 網(wǎng)頁不會(huì)被緩存 META HTTP-EQUIV=pragma CONTENT=no-cache META HTTP-EQUIV=Cache-Control CONTENT=no-cache, must-revalidate META HTTP-EQUIV=expires CONTENT=Wed, 26 Feb 1997 08:21:57 GMT 或者 META HTTP-EQUIV=expires CONTENT=0 19.怎樣讓表單沒有

28、凹凸感? input type=text style=border:1 solid #000000 或 input type=text style=border-left:none; border-right:none; border-top:none; border-bottom: 1 solid #000000 /textarea 20. div span& layer的區(qū)別? div(division)用來定義大段的頁面元素,會(huì)產(chǎn)生轉(zhuǎn)行 span用來定義同一行內(nèi)的元素,跟 div的唯一區(qū)別是不產(chǎn)生轉(zhuǎn)行 layer是ns的標(biāo)記,ie不支持,相當(dāng)于 div 21.讓彈出窗口總是在最上面: b

29、ody onblur=this.focus(); 22.不要滾動(dòng)條? 讓豎條沒有: body style=overflow:scroll;overflow-y:hidden /body 讓橫條沒有: body style=overflow:scroll;overflow-x:hidden /body 兩個(gè)都去掉?更簡單了 body scroll=no /body 23.怎樣去掉圖片鏈接點(diǎn)擊后,圖片周圍的虛線? a href=# onFocus=this.blur() img src=logo.jpg border=0 /a 24.電子郵件處理提交表單 form name=form1 metho

30、d=post action=mailto:*.com enctype=text/plain input type=submit /form25.在打開的子窗口刷新父窗口的代碼里如何寫? window.opener.location.reload() 26.如何設(shè)定打開頁面的大小 body onload=top.resizeTo(300,200); 打開頁面的位置 body onload=top.moveBy(300,200); 27.在頁面中如何加入不是滿鋪的背景圖片,拉動(dòng)頁面時(shí)背景圖不動(dòng) STYLE body background-image:url(logo.gif); backgroun

31、d-repeat:no-repeat; background-position:center;background-attachment: fixed /STYLE 28. 檢查一段字符串是否全由數(shù)字組成 script language=Javascript !- function checkNum(str)return str.match(/D/)=null alert(checkNum(1232142141) alert(checkNum(123214214a1) / - /script 29. 獲得一個(gè)窗口的大小 document.body.clientWidth; document.b

32、ody.clientHeight 30. 怎么判斷是否是字符 if (/x00-/xff/g.test(s) alert(含有漢字); else alert(全是字符); 31.TEXTAREA自適應(yīng)文字行數(shù)的多少 textarea rows=1 name=s1 cols=27 onpropertychange=this.style.posHeight=this.scrollHeight /textarea 32. 日期減去天數(shù)等于第二個(gè)日期 script language=Javascript function cc(dd,dadd) /可以加上錯(cuò)誤處理 var a = new Date(d

33、d) a = a.valueOf() a = a - dadd * 24 * 60 * 60 * 1000 a = new Date(a) alert(a.getFullYear() + 年 + (a.getMonth() + 1) + 月 + a.getDate() + 日) cc(12/23/2002,2) /script 33. 選擇了哪一個(gè)Radio HTML script language=vbscript function checkme() for each ob in radio1 if ob.checked then window.alert ob.value next en

34、d function /script BODY INPUT name=radio1 type=radio value=style checkedStyle INPUT name=radio1 type=radio value=barcodeBarcode INPUT type=button value=check onclick=checkme() /BODY /HTML 34.腳本永不出錯(cuò) SCRIPT LANGUAGE=JavaScript !- Hide function killErrors() return true; window.onerror = killErrors; / -

35、 /SCRIPT 35.ENTER鍵可以讓光標(biāo)移到下一個(gè)輸入框 input onkeydown=if(event.keyCode=13)event.keyCode=9 36. 檢測某個(gè)網(wǎng)站的鏈接速度: 把如下代碼加入 body區(qū)域中: script language=Javascript tim=1 setInterval(tim+,100) b=1 var autourl=new Array() autourl1= autourl2= autourl3= autourl4=.c

36、n autourl5= function butt() document.write( form name=autof) for(var i=1;i autourl.length;i+) document.write( input type=text name=txt+i+ size=10 value=測試中 = input type=text name=url+i+ size=40 = input type=button value=GO onclick=window.open(this.form.url+i+.value) ) document.write( input type=submit value=刷新 /form) butt() function auto(url) document.forms0url+b.value=url if(tim200) d

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論