




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Javascript 方法技巧整理大全Javascript方法技巧大全把有关JavaScript的方法和技巧整理下,让每个在为JavaScript而烦恼的人明白,JavaScript就这么回事!并希望JavaScript还能够成为您的朋友,让您豁然开朗,在项目中更好的应用适合阅读范围:对JavaScript一无所知离精通只差一步之遥的人基础知识:HTMLJavaScript就这么回事1:基础知识 1 创建脚本块1: 2: JavaScript code goes here3: 2 隐藏脚本代码1: 2: 5: 在不支持JavaScript的浏览器中将不执行相关代码3 浏览器不支持的时候显示1: 2: Hello to the non-JavaScript browser.3: 4 链接外部脚本文档1: 5 注释脚本1: / This is a comment2: document.write(“Hello”); / This is a comment3: /*4: All of this5: is a comment6: */ 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.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: 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 = “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 text2: Link text 19 在页面加载完成后执行函数1: 2: Body of the page3: 20 条件判断1: 2: 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 page3: JavaScript就这么回事2:浏览器输出 26 访问document对象1: 2: var myURL = document.URL;3: window.alert(myURL);4: 27 动态输出HTML1: 2: document.write(“Heres some information about this document:”);3: document.write(“”);4: document.write(“Referring Document: “ + document.referrer + “”);5: document.write(“Domain: “ + 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 currentDate = 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 配置日期输出格式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 urlParts = 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是无状态的么?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 Page7: 8: 9: This is the main page10: 11: JavaScript就这么回事3:图像 36 读取图像属性1: 2: Width3: 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.jpg”;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 = 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;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 = “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: 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:/somewhere.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 Choice5: Second Choice6: 7: 8: 9: 44 访问表单中的文本框内容1: 2: 3: 4: Check Text Field 45 动态复制文本框内容1: 2: Enter some Text: 3: Copy Text: 4: 5: Copy Text Field 46 侦测文本框的变化1: 2: Enter some Text: 3: 47 访问选中的Select1: 2: 3: 14: 25: 36: 7: 8: Check Selection List 48 动态增加Select项1: 2: 3: 14: 25: 6: 7: 8: document.myForm.mySelect.length+;9: document.myForm.mySelect.optionsdocument.myForm.mySelect.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: 12: 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 动态改变表单的action1: 2: Username: 3: Password: 4: 5: 6: 7: 52 使用图像按钮1: 2: Username: 3: Password: 4: 5: 6: 53 表单数据的加密1: 2: !-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) 4: document.write(“You chose OK”);5: else 6: document.write(“You chose Cancel”);7: 8: 56 提示输入1: 2: var userName = mpt(“Please Enter Your Name”,”Enter Your Name Here”);3: document.write(“Your Name is “ + userName);4: 57 打开一个新窗口1: /打开一个名称为myNewWindow的浏览器新窗口2: 3: window.open(“/”,”myNewWindow”);4: 58 配置新窗口的大小1: 2: window.open(“/”,”myNewWindow”,height=300,width=300);3: 59 配置新窗口的位置1: 2: window.open(“/”,”myNewWindow”,height=300,width=300,left=200,screenX=200,top=100,screenY=100);3: 60 是否显示工具栏和滚动栏1: 2: window.open(“http: 61 是否能够缩放新窗口的大小1: 2: window.open(/ , myNewWindow, resizable=yes ); 62 加载一个新的文档到当前窗口1: Open New Document 63 配置页面的滚动位置1: 2: if (document.all) /假如是IE浏览器则使用scrollTop属性3: document.body.scrollTop = 200;4: else /假如是NetScape浏览器则使用pag
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年海外代表处招聘笔试重点题
- 2025年美术设计师专业技能提升课程模拟考试题库及解析
- 2025年国际商务策划师资格考试试题及答案解析
- 2025年青少年宫招聘笔试答案解析
- 2025年C证安全员考试模拟题库答案解析
- 天上的街市课件教学设计
- 做纸袋教学课件怎么做
- 数位板教学课件怎么用
- 课件app排行榜教学课件
- 2025年学生铁路安全知识练习题含答案
- 初中七年级分班考试语文试卷及答案
- 解读幼儿园教育指导纲要
- 高效氯胺酮合成路线研究-深度研究
- 秘书工作中的职业发展规划研究论文
- 招标代理招标服务实施方案
- 垃圾清运合同范本模板建筑
- 合伙开公司必签的五份协议
- 八年级地理实验室使用计划
- 2024LNG储罐焊缝X射线数字成像检测规范
- DB5117T 22-2020 地理标志产品 米城大米
- 设计概论讲课课件(第三版杨晓琪)
评论
0/150
提交评论