版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Js基礎(chǔ)代碼1 創(chuàng)建腳本塊引用內(nèi)容程序代碼<script language=”JavaScript”>JavaScript 代碼寫(xiě)在這里面</script>2 隱藏腳本代碼引用內(nèi)容程序代碼<script language=”JavaScript”><!document.write(“Hello”);/ ></script>在不支持JavaScript的瀏覽器中將不執(zhí)行相關(guān)代碼3 瀏覽器不支持的時(shí)候顯示引用內(nèi)容程序代碼<noscript>Hello to the non-JavaScript browser.</nos
2、cript>4 鏈接外部腳本文件引用內(nèi)容程序代碼<script language=”JavaScript” src=”/”filename.js”></script>5 注釋腳本引用內(nèi)容程序代碼/ This is a commentdocument.write(“Hello”); / This is a comment/*All of thisis a comment*/6 輸出到瀏覽器引用內(nèi)容程序代碼document.write(“<strong>Hello</strong>”);7 定義變量引用內(nèi)容程序代碼var myVariable
3、= “some value”;8 字符串相加引用內(nèi)容程序代碼var myString = “String1” + “String2”;9 字符串搜索引用內(nèi)容程序代碼<script language=”JavaScript”><!var myVariable = “Hello there”;var therePlace = myVariable.search(“there”);document.write(therePlace);/ ></script>10 字符串替換引用內(nèi)容程序代碼thisVar.replace(“Monday”,”Friday”);11
4、格式化字串引用內(nèi)容程序代碼<script language=”JavaScript”><!var myVariable = “Hello there”;document.write(myVariable.big() + “<br>”);document.write(myVariable.blink() + “<br>”);document.write(myVariable.bold() + “<br>”);document.write(myVariable.fixed() + “<br>”);document.write(myV
5、ariable.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>”)
6、;document.write(myVariable.sup() + “<br>”);document.write(myVariable.toLowerCase() + “<br>”);document.write(myVariable.toUpperCase() + “<br>”);var firstString = “My String”;var finalString = firstString.bold().toLowerCase().fontcolor(“red”);/ ></script>12 創(chuàng)建數(shù)組引用內(nèi)容程序代碼<s
7、cript language=”JavaScript”><!var myArray = new Array(5);myArray0 = “First Entry”;myArray1 = “Second Entry”;myArray2 = “Third Entry”;myArray3 = “Fourth Entry”;myArray4 = “Fifth Entry”;var anotherArray = new Array(“First Entry”,”Second Entry”,”Third Entry”,”Fourth Entry”,”Fifth Entry”);/ >&l
8、t;/script>13 數(shù)組排序引用內(nèi)容程序代碼<script language=”JavaScript”><!var myArray = new Array(5);myArray0 = “z”;myArray1 = “c”;myArray2 = “d”;myArray3 = “a”;myArray4 = “q”;document.write(myArray.sort();/ ></script>14 分割字符串引用內(nèi)容程序代碼<script language=”JavaScript”><!var myVariable = “a,b
9、,c,d”;var stringArray = myVariable.split(“,”);document.write(stringArray0);document.write(stringArray1);document.write(stringArray2);document.write(stringArray3);/ ></script>15 彈出警告信息引用內(nèi)容程序代碼<script language=”JavaScript”><!window.alert(“Hello”);/ ></script>16 彈出確認(rèn)框引用內(nèi)容程序代碼
10、<script language=”JavaScript”><!var result = window.confirm(“Click OK to continue”);/ ></script>17 自定義函數(shù)引用內(nèi)容程序代碼<script language=”JavaScript”><!function multiple(number1,number2) var result = number1 * number2;return result;/ ></script>18 調(diào)用JS函數(shù)引用內(nèi)容程序代碼<a href=
11、”#” onClick=”functionName()”>Link text</a><a href=”/”javascript:functionName”()”>Link text</a>19 在頁(yè)面加載完成后執(zhí)行函數(shù)引用內(nèi)容程序代碼<body onLoad=”functionName();”>Body of the page</body>20 條件判斷引用內(nèi)容程序代碼<script><!var userChoice = window.confirm(“Choose OK or Cancel”);var res
12、ult = (userChoice = true) ? “OK” : “Cancel”;document.write(result);/ ></script>21 指定次數(shù)循環(huán)引用內(nèi)容程序代碼<script><!var myArray = new Array(3);myArray0 = “Item 0”;myArray1 = “Item 1”;myArray2 = “Item 2”;for (i = 0; i < myArray.length; i+) document.write(myArrayi + “<br>”);/ ><
13、/script>22 設(shè)定將來(lái)執(zhí)行引用內(nèi)容程序代碼<script><!function hello() window.alert(“Hello”);window.setTimeout(“hello()”,5000);/ ></script>23 定時(shí)執(zhí)行函數(shù)引用內(nèi)容程序代碼<script><!function hello() window.alert(“Hello”);window.setTimeout(“hello()”,5000);window.setTimeout(“hello()”,5000);/ ></scrip
14、t>24 取消定時(shí)執(zhí)行引用內(nèi)容程序代碼<script><!function hello() window.alert(“Hello”);var myTimeout = window.setTimeout(“hello()”,5000);window.clearTimeout(myTimeout);/ ></script>25 在頁(yè)面卸載時(shí)候執(zhí)行函數(shù)引用內(nèi)容程序代碼<body onUnload=”functionName();”>Body of the page</body>JavaScript就這么回事2:瀏覽器輸出26 訪(fǎng)問(wèn)d
15、ocument對(duì)象引用內(nèi)容程序代碼<script language=”JavaScript”>var myURL = document.URL;window.alert(myURL);</script>27 動(dòng)態(tài)輸出HTML引用內(nèi)容程序代碼<script language=”JavaScript”>document.write(“<p>Heres some information about this document:</p>”);document.write(“<ul>”);document.write(“<li
16、>Referring Document: “ + document.referrer + “</li>”);document.write(“<li>Domain: “ + document.domain + “</li>”);document.write(“<li>URL: “ + document.URL + “</li>”);document.write(“</ul>”);</script>28 輸出換行引用內(nèi)容程序代碼document.writeln(“<strong>a</str
17、ong>”);document.writeln(“b”);29 輸出日期引用內(nèi)容程序代碼<script language=”JavaScript”>var thisDate = new Date();document.write(thisDate.toString();</script>30 指定日期的時(shí)區(qū)引用內(nèi)容程序代碼<script language=”JavaScript”>var myOffset = -2;var currentDate = new Date();var userOffset = currentDate.getTimezone
18、Offset()/60;var timeZoneDifference = userOffset myOffset;currentDate.setHours(currentDate.getHours() + timeZoneDifference);document.write(“The time and date in Central Europe is: “ + currentDate.toLocaleString();</script>31 設(shè)置日期輸出格式引用內(nèi)容程序代碼<script language=”JavaScript”>var thisDate = new
19、 Date();var thisTimeString = thisDate.getHours() + “:” + thisDate.getMinutes();var thisDateString = thisDate.getFullYear() + “/” + thisDate.getMonth() + “/” + thisDate.getDate();document.write(thisTimeString + “ on “ + thisDateString);</script>32 讀取URL參數(shù)引用內(nèi)容程序代碼<script language=”JavaScript”
20、>var urlParts = document.URL.split(“?”);var parameterParts = urlParts1.split(“&”);for (i = 0; i < parameterParts.length; i+) var pairParts = parameterPartsi.split(“=”);var pairName = pairParts0;var pairValue = pairParts1;document.write(pairName + “ :“ +pairValue );</script>你還以為HTML是無(wú)
21、狀態(tài)的么?33 打開(kāi)一個(gè)新的document對(duì)象引用內(nèi)容程序代碼<script language=”JavaScript”>function newDocument() document.open();document.write(“<p>This is a New Document.</p>”);document.close();</script>34 頁(yè)面跳轉(zhuǎn)引用內(nèi)容程序代碼<script language=”JavaScript”>window.location = “http:/www.x-</script>35
22、添加網(wǎng)頁(yè)加載進(jìn)度窗口引用內(nèi)容程序代碼<html><head><script language=javaScript>var placeHolder = window.open(holder.html,'placeholder,'width=200,height=200);</script><title>The Main Page</title></head><body onLoad=placeHolder.close()><p>This is the main page&
23、lt;/p></body></html>JavaScript就這么回事3:圖像36 讀取圖像屬性引用內(nèi)容程序代碼<img src=”/”image1.jpg” name=”myImage”><a href=”# ” onClick=”window.alert(document.myImage.width)”>Width</a>37 動(dòng)態(tài)加載圖像引用內(nèi)容程序代碼<script language=”JavaScript”>myImage = new Image;myImage.src = “Tellers1.jpg”;&
24、lt;/script>38 簡(jiǎn)單的圖像替換引用內(nèi)容程序代碼<script language=”JavaScript”>rollImage = new Image;rollImage.src = “rollImage1.jpg”;defaultImage = new Image;defaultImage.src = “image1.jpg”;</script><a href=”/”myUrl” onMouseOver=”document.myImage.src = rollImage.src;”onMouseOut=”document.myImage.src
25、= defaultImage.src;”><img src=”/”image1.jpg” name=”myImage” width=100 height=100 border=0>39 隨機(jī)顯示圖像引用內(nèi)容程序代碼<script language=”JavaScript”>var imageList = new Array;imageList0 = “image1.jpg”;imageList1 = “image2.jpg”;imageList2 = “image3.jpg”;imageList3 = “image4.jpg”;var imageChoice =
26、Math.floor(Math.random() * imageList.length);document.write(<img src=” + imageListimageChoice + “>);</script>40 函數(shù)實(shí)現(xiàn)的圖像替換引用內(nèi)容程序代碼<script language=”JavaScript”>var source = 0;var replacement = 1;function createRollOver(originalImage,replacementImage) var imageArray = new Array;image
27、Arraysource = new Image;imageArraysource.src = originalImage;imageArrayreplacement = new Image;imageArrayreplacement.src = replacementImage;return imageArray;var rollImage = createRollOver(“image1.jpg”,”rollImage1.jpg”);</script><a href=”#” onMouseOver=”document.myImage1.src = rollImage1rep
28、lacement.src;”onMouseOut=”document.myImage1.src = rollImage1source.src;”><img src=”/”image1.jpg” width=100 name=”myImage1” border=0></a>41 創(chuàng)建幻燈片引用內(nèi)容程序代碼<script language=”JavaScript”>var imageList = new Array;imageList0 = new Image;imageList0.src = “image1.jpg”;imageList1 = new I
29、mage;imageList1.src = “image2.jpg”;imageList2 = new Image;imageList2.src = “image3.jpg”;imageList3 = new Image;imageList3.src = “image4.jpg”;function slideShow(imageNumber) document.slideShow.src = imageListimageNumber.src;imageNumber += 1;if (imageNumber < imageList.length) window.setTimeout(“sl
30、ideShow(“ + imageNumber + “)”,3000);</script></head><body onLoad=”slideShow(0)”><img src=”/”image1.jpg” width=100 name=”slideShow”>42 隨機(jī)廣告圖片引用內(nèi)容程序代碼<script language=”JavaScript”>var imageList = new Array;imageList0 = “image1.jpg”;imageList1 = “image2.jpg”;imageList2 = “
31、image3.jpg”;imageList3 = “image4.jpg”;var urlList = new Array;urlList0 = “http:/some.host/”;urlList1 = “http:/another.host/”;urlList2 = “http:/somewhere.else/”;urlList3 = “http:/right.here/”;var imageChoice = Math.floor(Math.random() * imageList.length);document.write(<a href=” + urlListimageChoi
32、ce + “><img src=” + imageListimageChoice + “></a>);</script>JavaScript就這么回事4:表單43 表單構(gòu)成引用內(nèi)容程序代碼<form method=”post” action=”target.html” name=”thisForm”><input type=”text” name=”myText”><select name=”mySelect”><option value=”1”>First Choice</option>&l
33、t;option value=”2”>Second Choice</option></select><br><input type=”submit” value=”Submit Me”></form>44 訪(fǎng)問(wèn)表單中的文本框內(nèi)容引用內(nèi)容程序代碼<form name=”myForm”><input type=”text” name=”myText”></form><a href=# onClick=window.alert(document.myForm.myText.value);>
34、;Check Text Field</a>45 動(dòng)態(tài)復(fù)制文本框內(nèi)容引用內(nèi)容程序代碼<form name=”myForm”>Enter some Text: <input type=”text” name=”myText”><br>Copy Text: <input type=”text” name=”copyText”></form><a href=”#” onClick=”document.myForm.copyText.value =document.myForm.myText.value;”>Copy T
35、ext Field</a>46 偵測(cè)文本框的變化引用內(nèi)容程序代碼<form name=”myForm”>Enter some Text: <input type=”text” name=”myText” onChange=”alert(this.value);”></form>47 訪(fǎng)問(wèn)選中的Select引用內(nèi)容程序代碼<form name=”myForm”><select name=”mySelect”><option value=”First Choice”>1</option><opti
36、on value=”Second Choice”>2</option><option value=”Third Choice”>3</option></select></form><a href=# onClick=alert(document.myForm.mySelect.value);>Check Selection List</a>48 動(dòng)態(tài)增加Select項(xiàng)引用內(nèi)容程序代碼<form name=”myForm”><select name=”mySelect”><op
37、tion value=”First Choice”>1</option><option value=”Second Choice”>2</option></select></form><script language=”JavaScript”>document.myForm.mySelect.length+;document.myForm.mySelect.optionsdocument.myForm.mySelect.length - 1.text = “3”;document.myForm.mySelect.op
38、tionsdocument.myForm.mySelect.length - 1.value = “Third Choice”;</script>49 驗(yàn)證表單字段引用內(nèi)容程序代碼<script language=”JavaScript”>function checkField(field) if (field.value = “”) window.alert(“You must enter a value in the field”);field.focus();</script><form name=”myForm” action=”target.
39、html”>Text Field: <input type=”text” name=”myField”onBlur=”checkField(this)”><br><input type=”submit”></form>50 驗(yàn)證Select項(xiàng)引用內(nèi)容程序代碼function checkList(selection) if (selection.length = 0) window.alert(“You must make a selection from the list.”);return false;return true;51 動(dòng)態(tài)改
40、變表單的action引用內(nèi)容程序代碼<form name=”myForm” action=”login.html”>Username: <input type=”text” name=”username”><br>Password: <input type=”password” name=”password”><br><input type=”button” value=”Login” onClick=”this.form.submit();”><input type=”button” value=”Register
41、” onClick=”this.form.action = register.html; this.form.submit();”><input type=”button” value=”Retrieve Password” onClick=”this.form.action = password.html; this.form.submit();”></form>52 使用圖像按鈕引用內(nèi)容程序代碼<form name=”myForm” action=”login.html”>Username: <input type=”text” name=”
42、username”><br>Password: <input type=”password”name=”password”><br><input type=”image” src=”/”login.gif” value=”Login”></form>53 表單數(shù)據(jù)的加密引用內(nèi)容程序代碼<SCRIPT LANGUAGE=JavaScript><!function encrypt(item) var newItem = ”;for (i=0; i < item.length; i+) newItem += i
43、tem.charCodeAt(i) + .;return newItem;function encryptForm(myForm) for (i=0; i < myForm.elements.length; i+) myForm.elementsi.value = encrypt(myForm.elementsi.value);/></SCRIPT><form name=myForm onSubmit=encryptForm(this); window.alert(this.myField.value);>Enter Some Text: <input
44、 type=text name=myField><input type=submit></form>JavaScript就這么回事5:窗口和框架54 改變?yōu)g覽器狀態(tài)欄文字提示引用內(nèi)容程序代碼<script language=”JavaScript”>window.status = “A new status message”;</script>55 彈出確認(rèn)提示框引用內(nèi)容程序代碼<script language=”JavaScript”>var userChoice = window.confirm(“Click OK or C
45、ancel”);if (userChoice) document.write(“You chose OK”); else document.write(“You chose Cancel”);</script>56 提示輸入引用內(nèi)容程序代碼<script language=”JavaScript”>var userName = mpt(“Please Enter Your Name”,”Enter Your Name Here”);document.write(“Your Name is “ + userName);</script>57
46、 打開(kāi)一個(gè)新窗口引用內(nèi)容/打開(kāi)一個(gè)名稱(chēng)為myNewWindow的瀏覽器新窗口程序代碼<script language=”JavaScript”>window.open(“</script>58 設(shè)置新窗口的大小引用內(nèi)容程序代碼<script language=”JavaScript”>window.open(“</script>59 設(shè)置新窗口的位置引用內(nèi)容程序代碼<script language=”JavaScript”>window.open(“</script>60 是否顯示工具欄和滾動(dòng)欄引用內(nèi)容程序代碼<sc
47、ript language=”JavaScript”>window.open(“http:/www.x- menubar=no);</script>61 是否可以縮放新窗口的大小引用內(nèi)容程序代碼<script language=”JavaScript”>window.open(http:/www.x- , myNewWindow, resizable=yes );</script>62 加載一個(gè)新的文檔到當(dāng)前窗口引用內(nèi)容程序代碼<a href=# onClick=document.location = 125a.html; >Open New Document</a>63 設(shè)置頁(yè)面的滾動(dòng)位置引用內(nèi)容程序代碼<script language=”JavaScript”>if (document.all) /如果是IE瀏覽器則使用scrollTop屬性document.body.sc
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 新員工培訓(xùn)獎(jiǎng)懲管理辦法
- 綜合專(zhuān)項(xiàng)應(yīng)急演練培訓(xùn)
- 預(yù)防普通感冒的措施
- 自制英語(yǔ)課件教學(xué)課件
- 神秘的宇宙培訓(xùn)宣傳
- 第四章 曲線(xiàn)運(yùn)動(dòng)-教材實(shí)驗(yàn)5 探究平拋運(yùn)動(dòng)的特點(diǎn) 2025年高考物理基礎(chǔ)專(zhuān)項(xiàng)復(fù)習(xí)
- 防臺(tái)防汛安全教育培訓(xùn)
- 繁育員的年終總結(jié)
- 深度學(xué)習(xí)及自動(dòng)駕駛應(yīng)用 課件 第1、2章 汽車(chē)自動(dòng)駕駛技術(shù)概述、深度學(xué)習(xí)基礎(chǔ)
- 花樣跳繩說(shuō)課稿小學(xué)
- 2024年江西省公務(wù)員錄用考試《行測(cè)》題(網(wǎng)友回憶版)(題目及答案解析)
- VDA6.3基礎(chǔ)培訓(xùn)考核測(cè)試卷附答案
- 第01講 正數(shù)和負(fù)數(shù)、有理數(shù)-人教版新七年級(jí)《數(shù)學(xué)》暑假自學(xué)提升講義(解析版)
- 信息系統(tǒng)部署與運(yùn)維-題庫(kù)帶答案
- 婚姻心理學(xué)解讀包含內(nèi)容
- DZ/T 0462.3-2023 礦產(chǎn)資源“三率”指標(biāo)要求 第3部分:鐵、錳、鉻、釩、鈦(正式版)
- 備戰(zhàn)2024年高考英語(yǔ)考試易錯(cuò)點(diǎn)12 名詞性從句(4大陷阱)(解析版)
- 公務(wù)員歷史常識(shí)100題及一套完整答案
- 信息技術(shù)與高中英語(yǔ)教學(xué)融合的途徑
- 花籃拉桿式懸挑腳手架.計(jì)算書(shū)及相關(guān)圖紙
- 職業(yè)道德與法律說(shuō)課稿市公開(kāi)課一等獎(jiǎng)省賽課微課金獎(jiǎng)?wù)n件
評(píng)論
0/150
提交評(píng)論