版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、JavaScript方法和技巧大全來(lái)源:適合閱讀范圍:對(duì)JavaScript一無(wú)所知離精通只差一步之遙的人基礎(chǔ)知識(shí):HTMLJavaScript就這么回事1:基礎(chǔ)知識(shí) 1 創(chuàng)建腳本塊1: <script language=”JavaScript”>2: JavaScript code goes here3: </script> 2 隱藏腳本代碼1: <script language=”JavaScript”>2: <!-3: document.write(“Hello”);4: / ->5: </script> 在不支持J
2、avaScript的瀏覽器中將不執(zhí)行相關(guān)代碼3 瀏覽器不支持的時(shí)候顯示1: <noscript>2: Hello to the non-JavaScript browser.3: </noscript> 4 鏈接外部腳本文件1: <script language=”JavaScript” src="/”filename.js"”></script> 5 注釋腳本1: / This is a comment2: document.write(“Hello”); / This is a comment3: /*4: A
3、ll of this5: is a comment6: */ 6 輸出到瀏覽器1: document.write(“<strong>Hello</strong>”); 7 定義變量1: var myVariable = “some value”; 8 字符串相加1: var myString = “String1” + “String2”; 9 字符串搜索1: <script language=”JavaScript”>2: <!-3: var myVariable = “Hello there”;4:
4、var therePlace = myVariable.search(“there”);5: document.write(therePlace);6: / ->7: </script> 10 字符串替換1: thisVar.replace(“Monday”,”Friday”); 11 格式化字串1: <script language=”JavaScript”>2: <!-3: var myVariable = “Hello there”;4: document.write(myVariable.big() + “<br>”);5:
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(my
6、Variable.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() + “<b
7、r>”);16: document.write(myVariable.toUpperCase() + “<br>”);17: 18: var firstString = “My String”;19: var finalString = firstString.bold().toLowerCase().fontcolor(“red”);20: / ->21: </script> 12 創(chuàng)建數(shù)組1: <script language=”JavaScript”>2: <!-3: var myArray = new Array(5);
8、4: myArray0 = “First Entry”;5: myArray1 = “Second Entry”;6: myArray2 = “Third Entry”;7: myArray3 = “Fourth Entry”;8: myArray4 = “Fifth Entry”;9: var anotherArray = new Array(“First Entry”,”Second Entry”,”Third Entry”,”Fourth Entry”,”Fifth Entry”);10: / ->11: </script> 13 數(shù)組排序1: <sc
9、ript language=”JavaScript”>2: <!-3: var myArray = new Array(5);4: myArray0 = “z”;5: myArray1 = “c”;6: myArray2 = “d”;7: myArray3 = “a”;8: myArray4 = “q”;9: document.write(myArray.sort();10: / ->11: </script> 14 分割字符串1: <script language=”JavaScript”>2: <!-3: var myVariab
10、le = “a,b,c,d”;4: var stringArray = myVariable.split(“,”);5: document.write(stringArray0);6: document.write(stringArray1);7: document.write(stringArray2);8: document.write(stringArray3);9: / ->10: </script> 15 彈出警告信息1: <script language=”JavaScript”>2: <!-3: window.alert(“Hell
11、o”);4: / ->5: </script> 16 彈出確認(rèn)框1: <script language=”JavaScript”>2: <!-3: var result = window.confirm(“Click OK to continue”);4: / ->5: </script> 17 定義函數(shù)1: <script language=”JavaScript”>2: <!-3: function multiple(number1,number2) 4: var result = number
12、1 * number2;5: return result;6: 7: / ->8: </script> 18 調(diào)用JS函數(shù)1: <a href=”#” onClick=”functionName()”>Link text</a>2: <a href="/”javascript:functionName"()”>Link text</a> 19 在頁(yè)面加載完成后執(zhí)行函數(shù)1: <body onLoad=”functionName();”>2: Body of the page3
13、: </body> 20 條件判斷1: <script>2: <!-3: var userChoice = window.confirm(“Choose OK or Cancel”);4: var result = (userChoice = true) ? “OK” : “Cancel”;5: document.write(result);6: / ->7: </script> 21 指定次數(shù)循環(huán)1: <script>2: <!-3: var myArray = new Array(3);4: myArray0 =
14、 “Item 0”;5: myArray1 = “Item 1”;6: myArray2 = “Item 2”;7: for (i = 0; i < myArray.length; i+) 8: document.write(myArrayi + “<br>”);9: 10: / ->11: </script> 22 設(shè)定將來(lái)執(zhí)行1: <script>2: <!-3: function hello() 4: window.alert(“Hello”);5: 6: window.setTimeout(“hello()”,5000)
15、;7: / ->8: </script> 23 定時(shí)執(zhí)行函數(shù)1: <script>2: <!-3: function hello() 4: window.alert(“Hello”);5: window.setTimeout(“hello()”,5000);6: 7: window.setTimeout(“hello()”,5000);8: / ->9: </script> 24 取消定時(shí)執(zhí)行1: <script>2: <!-3: function hello() 4: window.alert(“
16、Hello”);5: 6: var myTimeout = window.setTimeout(“hello()”,5000);7: window.clearTimeout(myTimeout);8: / ->9: </script> 25 在頁(yè)面卸載時(shí)候執(zhí)行函數(shù)1: <body onUnload=”functionName();”>2: Body of the page3: </body> JavaScript就這么回事2:瀏覽器輸出 26 訪問(wèn)document對(duì)象1: <script language=”JavaScript”&g
17、t;2: var myURL = document.URL;3: window.alert(myURL);4: </script> 27 動(dòng)態(tài)輸出HTML1: <script language=”JavaScript”>2: document.write(“<p>Heres some information about this document:</p>”);3: document.write(“<ul>”);4: document.write(“<li>Referring Document: “ + doc
18、ument.referrer + “</li>”);5: document.write(“<li>Domain: “ + document.domain + “</li>”);6: document.write(“<li>URL: “ + document.URL + “</li>”);7: document.write(“</ul>”);8: </script> 28 輸出換行1: document.writeln(“<strong>a</strong>”);2: document.w
19、riteln(“b”); 29 輸出日期1: <script language=”JavaScript”>2: var thisDate = new Date();3: document.write(thisDate.toString();4: </script> 30 指定日期的時(shí)區(qū)1: <script language=”JavaScript”>2: var myOffset = -2;3: var currentDate = new Date();4: var userOffset = currentDate.getTimezo
20、neOffset()/60;5: var timeZoneDifference = userOffset - myOffset;6: currentDate.setHours(currentDate.getHours() + timeZoneDifference);7: document.write(“The time and date in Central Europe is: “ + currentDate.toLocaleString();8: </script> 31 設(shè)置日期輸出格式1: <script language=”JavaScript”>2: var
21、 thisDate = new Date();3: var thisTimeString = thisDate.getHours() + “:” + thisDate.getMinutes();4: var thisDateString = thisDate.getFullYear() + “/” + thisDate.getMonth() + “/” + thisDate.getDate();5: document.write(thisTimeString + “ on “ + thisDateString);6: </script> 32 讀取URL參數(shù)你還以為HTML是無(wú)狀態(tài)
22、的么?33 打開(kāi)一個(gè)新的document對(duì)象1: <script language=”JavaScript”>2: function newDocument() 3: document.open();4: document.write(“<p>This is a New Document.</p>”);5: document.close();6: 7: </script> 34 頁(yè)面跳轉(zhuǎn) 35 添加網(wǎng)頁(yè)加載進(jìn)度窗口1: <html>2: <head>3: <script language=
23、9;javaScript'>4: var placeHolder = window.open('holder.html','placeholder','width=200,height=200');5: </script>6: <title>The Main Page</title>7: </head>8: <body onLoad='placeHolder.close()'>9: <p>This is the main page</p&g
24、t;10: </body>11: </html> JavaScript就這么回事3:圖像 36 讀取圖像屬性37 動(dòng)態(tài)加載圖像1: <script language=”JavaScript”>2: myImage = new Image;3: myImage.src = “Tellers1.jpg”;4: </script> 38 簡(jiǎn)單的圖像替換39 隨機(jī)顯示圖像1: <script language=”JavaScript”>2: var imageList = new Array;3: imageList0
25、 = “image1.jpg”;4: imageList1 = “image2.jpg”;5: imageList2 = “image3.jpg”;6: imageList3 = “image4.jpg”;7: var imageChoice = Math.floor(Math.random() * imageList.length);8: document.write(<img src=” + imageListimageChoice + “>);9: </script> 40 函數(shù)實(shí)現(xiàn)的圖像替換41 創(chuàng)建幻燈片42 隨機(jī)廣告圖片1: <script langu
26、age=”JavaScript”>2: var imageList = new Array;3: imageList0 = “image1.jpg”;4: imageList1 = “image2.jpg”;5: imageList2 = “image3.jpg”;6: imageList3 = “image4.jpg”;7: var urlList = new Array;8: urlList0 = “http:/some.host/”;9: urlList1 = “http:/another.host/”;10: urlList2 = “http:/somewhere.else/”;
27、11: urlList3 = “http:/right.here/”;12: var imageChoice = Math.floor(Math.random() * imageList.length);13: document.write(<a href=” + urlListimageChoice + “><img src=” + imageListimageChoice + “></a>);14: </script> JavaScript就這么回事4:表單 還是先繼續(xù)寫(xiě)完JS就這么回事系列吧43 表單構(gòu)成1: <form method
28、=”post” action=”target.html” name=”thisForm”>2: <input type=”text” name=”myText”>3: <select name=”mySelect”>4: <option value=”1”>First Choice</option>5: <option value=”2”>Second Choice</option>6: </select>7: <br>8: <input type=”submit” value=”Sub
29、mit Me”>9: </form> 44 訪問(wèn)表單中的文本框內(nèi)容45 動(dòng)態(tài)復(fù)制文本框內(nèi)容46 偵測(cè)文本框的變化1: <form name=”myForm”>2: Enter some Text: <input type=”text” name=”myText” onChange=”alert(this.value);”>3: </form> 47 訪問(wèn)選中的Select48 動(dòng)態(tài)增加Select項(xiàng)49 驗(yàn)證表單字段1: <script language=”JavaScript”>2: function checkField(f
30、ield) 3: if (field.value = “”) 4: window.alert(“You must enter a value in the field”);5: field.focus();6: 7: 8: </script>9: <form name=”myForm” action=”target.html”>10: Text Field: <input type=”text” name=”myField”onBlur=”checkField(this)”>11: <br><input type=”submit”>1
31、2: </form> 50 驗(yàn)證Select項(xiàng)1: function checkList(selection) 2: if (selection.length = 0) 3: window.alert(“You must make a selection from the list.”);4: return false;5: 6: return true;7: 51 動(dòng)態(tài)改變表單的action52 使用圖像按鈕1: <form name=”myForm” action=”login.html”>2: Username: <input type=”text” nam
32、e=”username”><br>3: Password: <input type=”password”name=”password”><br>4: <input type=”image” src="/”login.gif"” value=”Login”>5: </form>6: 53 表單數(shù)據(jù)的加密 JavaScript就這么回事5:窗口和框架 54 改變?yōu)g覽器狀態(tài)欄文字提示1: <script language=”JavaScript”>2: window.status = “A n
33、ew status message”;3: </script> 55 彈出確認(rèn)提示框1: <script language=”JavaScript”>2: var userChoice = window.confirm(“Click OK or Cancel”);3: if (userChoice) 4: document.write(“You chose OK”);5: else 6: document.write(“You chose Cancel”);7: 8: </script> 56 提示輸入1: <script language=”JavaScript”>2: var userName = mpt(“Please Enter Your Name”,”Enter Your Name Here”);3: document.write(“Your Name is
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 感恩老師心得體會(huì)15篇
- 國(guó)土分局業(yè)務(wù)知識(shí)
- 醫(yī)務(wù)人員洗手相關(guān)知識(shí)
- 正頜手術(shù)加速康復(fù)外科臨床路徑指標(biāo)體系構(gòu)建及對(duì)術(shù)后康復(fù)效果影響
- 基于深度學(xué)習(xí)的PMU異常數(shù)據(jù)檢測(cè)方法研究
- 二零二五年度綠色環(huán)保消防設(shè)施安裝與維護(hù)合同協(xié)議書(shū)3篇
- 2025版水果種植基地與冷鏈物流企業(yè)合作協(xié)議范本3篇
- 臭氧水療聯(lián)合皮膚科特色護(hù)理治療濕疹患兒的價(jià)值
- 快遞行業(yè)培訓(xùn)計(jì)劃
- xx市科創(chuàng)孵化器項(xiàng)目可行性研究報(bào)告
- 人教版八年級(jí)英語(yǔ)上冊(cè)期末專(zhuān)項(xiàng)復(fù)習(xí)-完形填空和閱讀理解(含答案)
- 一例蛇串瘡患者個(gè)案護(hù)理課件
- 低壓電工理論考試題庫(kù)低壓電工考試題
- 駱駝祥子選擇題100道及答案
- 2024年公務(wù)員考試題庫(kù)附答案【完整版】
- T-GDWCA 0019-2018 輻照工藝操作規(guī)范
- 司機(jī)考核管理制度
- 出差報(bào)銷(xiāo)單-中英對(duì)照版
- 【學(xué)前教育小學(xué)化成因分析及其對(duì)策10000字(論文)】
- 腕管綜合征課件
- 事業(yè)單位工作人員年度考核登記表(通用模板)
評(píng)論
0/150
提交評(píng)論