《javasript 函数 2(42页).doc》由会员分享,可在线阅读,更多相关《javasript 函数 2(42页).doc(42页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-javasript 函数 2-第 42 页JavaScript就这么回事1:基础知识 1 创建脚本块 1: 2: JavaScript code goes here 3: 2 隐藏脚本代码 1: 2: 5: 在不支持JavaScript的浏览器中将不执行相关代码 3 浏览器不支持的时候显示 1: 2: Hello to the non-JavaScript browser. 3: 4 链接外部脚本文件 1: 5 注释脚本 1: / This is a comment 2: document.write(“Hello”); / This is a comment 3: /* 4: All of
2、 this 5: is a comment 6: */ 6 输出到浏览器 1: document.write(“Hello”); 7 定义变量 1: var myVariable = “some value”; 8 字符串相加 1: var myString = “String1” + “String2”; 9 字符串搜索 1: 2: 7: 10 字符串替换 1: thisVar.replace(“Monday”,”Friday”); 11 格式化字串 1: 2: ! 3: var myVariable = “Hello there”; 4: document.write(myVariable
3、.big() + “”); 5: document.write(myVariable.blink() + “”); 6: document.write(myVariable.bold() + “”); 7: document.write(myVariable.fixed() + “”); 8: document.write(myVariable.fontcolor(“red”) + “”); 9: document.write(myVariable.fontsize(“18pt”) + “”); 10: document.write(myVariable.italics() + “”); 11
4、: document.write(myVariable.small() + “”); 12: document.write(myVariable.strike() + “”); 13: document.write(myVariable.sub() + “”); 14: document.write(myVariable.sup() + “”); 15: document.write(myVariable.toLowerCase() + “”); 16: document.write(myVariable.toUpperCase() + “”); 17: 18: var firstString
5、 = “My String”; 19: var finalString = firstString.bold().toLowerCase().fontcolor(“red”); 20: / 21: 12 创建数组 1: 2: 11: 13 数组排序 1: 2: 11: 14 分割字符串 1: 2: 10: 15 弹出警告信息 1: 2: 5: 16 弹出确认框 1: 2: 5: 17 定义函数 1: 2: 8: 18 调用JS函数 1: Link text 2: Link text 19 在页面加载完成后执行函数 1: 2: Body of the page 3: 20 条件判断 1: 2:
6、7: 21 指定次数循环 1: 2: ! 3: var myArray = new Array(3); 4: myArray0 = “Item 0”; 5: myArray1 = “Item 1”; 6: myArray2 = “Item 2”; 7: for (i = 0; i myArray.length; i+) 8: document.write(myArrayi + “”); 9: 10: / 11: 22 设定将来执行 1: 2: 8: 23 定时执行函数 1: 2: 9: 24 取消定时执行 1: 2: 9: 25 在页面卸载时候执行函数 1: 2: Body of the pa
7、ge 3: JavaScript就这么回事2:浏览器输出 26 访问document对象 1: 2: var myURL = document.URL; 3: window.alert(myURL); 4: 27 动态输出HTML 1: 2: document.write(“Heres some information about this document:”); 3: document.write(“”); 4: document.write(“Referring Document: “ + document.referrer + “”); 5: document.write(“Domai
8、n: “ + document.domain + “”); 6: document.write(“URL: “ + document.URL + “”); 7: document.write(“”); 8: 28 输出换行 1: document.writeln(“a”); 2: document.writeln(“b”); 29 输出日期 1: 2: var thisDate = new Date(); 3: document.write(thisDate.toString(); 4: 30 指定日期的时区 1: 2: var myOffset = -2; 3: var currentDat
9、e = new Date(); 4: var userOffset = currentDate.getTimezoneOffset()/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: 31 设置日期输出格式
10、 1: 2: var 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: 32 读取URL参数 1: 2: var url
11、Parts = document.URL.split(“?”); 3: var parameterParts = urlParts1.split(“&”); 4: for (i = 0; i parameterParts.length; i+) 5: var pairParts = parameterPartsi.split(“=”); 6: var pairName = pairParts0; 7: var pairValue = pairParts1; 8: document.write(pairName + “ :“ +pairValue ); 9: 10: 你还以为HTML是无状态的么
12、? 33 打开一个新的document对象 1: 2: function newDocument() 3: document.open(); 4: document.write(“This is a New Document.”); 5: document.close(); 6: 7: 34 页面跳转 1: 2: window.location = “”; 3: 35 添加网页加载进度窗口 1: 2: 3: 4: var placeHolder = window.open(holder.html,placeholder,width=200,height=200); 5: 6: The Main
13、 Page 7: 8: 9: This is the main page 10: 11: JavaScript就这么回事3:图像 36 读取图像属性 1: 2: Width 3: 37 动态加载图像 1: 2: myImage = new Image; 3: myImage.src = “Tellers1.jpg”; 4: 38 简单的图像替换 1: 2: rollImage = new Image; 3: rollImage.src = “rollImage1.jpg”; 4: defaultImage = new Image; 5: defaultImage.src = “image1.j
14、pg”; 6: 7: 9: 39 随机显示图像 1: 2: var imageList = new Array; 3: imageList0 = “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(); 9: 40 函数实现的图像替换 1: 2: var source =
15、0; 3: var replacement = 1; 4: function createRollOver(originalImage,replacementImage) 5: var imageArray = new Array; 6: imageArraysource = new Image; 7: imageArraysource.src = originalImage; 8: imageArrayreplacement = new Image; 9: imageArrayreplacement.src = replacementImage; 10: return imageArray;
16、 11: 12: var rollImage1 = createRollOver(“image1.jpg”,”rollImage1.jpg”); 13: 14: 16: 17: 41 创建幻灯片 1: 2: var imageList = new Array; 3: imageList0 = new Image; 4: imageList0.src = “image1.jpg”; 5: imageList1 = new Image; 6: imageList1.src = “image2.jpg”; 7: imageList2 = new Image; 8: imageList2.src =
17、“image3.jpg”; 9: imageList3 = new Image; 10: imageList3.src = “image4.jpg”; 11: function slideShow(imageNumber) 12: document.slideShow.src = imageListimageNumber.src; 13: imageNumber += 1; 14: if (imageNumber imageList.length) 15: window.setTimeout(“slideShow(“ + imageNumber + “)”,3000); 16: 17: 18:
18、 19: 20: 21: 42 随机广告图片 1: 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:/som
19、ewhere.else/”; 11: urlList3 = “http:/right.here/”; 12: var imageChoice = Math.floor(Math.random() * imageList.length); 13: document.write(); 14: JavaScript就这么回事4:表单 还是先继续写完JS就这么回事系列吧 43 表单构成 1: 2: 3: 4: First Choice 5: Second Choice 6: 7: 8: 9: 44 访问表单中的文本框内容 1: 2: 3: 4: Check Text Field 45 动态复制文本框内
20、容 1: 2: Enter some Text: 3: Copy Text: 4: 5: Copy Text Field 46 侦测文本框的变化 1: 2: Enter some Text: 3: 47 访问选中的Select 1: 2: 3: 1 4: 2 5: 3 6: 7: 8: Check Selection List 48 动态增加Select项 1: 2: 3: 1 4: 2 5: 6: 7: 8: document.myForm.mySelect.length+; 9: document.myForm.mySelect.optionsdocument.myForm.mySelec
21、t.length - 1.text = “3”; 10: document.myForm.mySelect.optionsdocument.myForm.mySelect.length - 1.value = “Third Choice”; 11: 49 验证表单字段 1: 2: function checkField(field) 3: if (field.value = “”) 4: window.alert(“You must enter a value in the field”); 5: field.focus(); 6: 7: 8: 9: 10: Text Field: 11: 1
22、2: 50 验证Select项 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 动态改变表单的action 1: 2: Username: 3: Password: 4: 5: 6: 7: 52 使用图像按钮 1: 2: Username: 3: Password: 4: 5: 6: 53 表单数据的加密 1: 2:
23、 ! 3: function encrypt(item) 4: var newItem = ; 5: for (i=0; i item.length; i+) 6: newItem += item.charCodeAt(i) + .; 7: 8: return newItem; 9: 10: function encryptForm(myForm) 11: for (i=0; i 17: 18: 19: Enter Some Text: 20: JavaScript就这么回事5:窗口和框架 54 改变浏览器状态栏文字提示 1: 2: window.status = “A new status message”; 3: 55 弹出确认提示框 1: 2: var userChoice = window.confirm(“Click OK or Cancel”); 3: if (userChoice)