JavaScript常用代码总结.doc_第1页
JavaScript常用代码总结.doc_第2页
JavaScript常用代码总结.doc_第3页
JavaScript常用代码总结.doc_第4页
JavaScript常用代码总结.doc_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

JavaScript常用代码总结1 创建脚本块JavaScript code goes here2 隐藏脚本代码在不支持JavaScript的浏览器中将不执行相关代码3 浏览器不支持的时候显示Hello to the non-JavaScript browser.4 链接外部脚本文件5 注释脚本/ This is a commentdocument.write(Hello); / This is a comment/*All of thisis a comment*/6 输出到浏览器document.write(Hello);7 定义变量var myVariable = some value;8 字符串相加var myString = String1 + String2;9 字符串搜索10 字符串替换thisVar.replace(Monday,Friday);11 格式化字串!-var myVariable = Hello there;document.write(myVariable.big() + );document.write(myVariable.blink() + );document.write(myVariable.bold() + );document.write(myVariable.fixed() + );document.write(myVariable.fontcolor(red) + );document.write(myVariable.fontsize(18pt) + );document.write(myVariable.italics() + );document.write(myVariable.small() + );document.write(myVariable.strike() + );document.write(myVariable.sub() + );document.write(myVariable.sup() + );document.write(myVariable.toLowerCase() + );document.write(myVariable.toUpperCase() + );17:var firstString = My String;var finalString = firstString.bold().toLowerCase().fontcolor(red);/ -212 创建数组113 数组排序114 分割字符串15 弹出警告信息16 弹出确认框17 定义函数18 调用JS函数Link textLink text19 在页面加载完成后执行函数Body of the page20 条件判断21 指定次数循环!-var myArray = new Array(3);myArray0 = Item 0;myArray1 = Item 1;myArray2 = Item 2;for (i = 0; i myArray.length; i+) document.write(myArrayi + );/ -22 设定将来执行23 定时执行函数24 取消定时执行25 在页面卸载时候执行函数Body of the pageJavaScript就这么回事2:浏览器输出26 访问document对象var myURL = document.URL;window.alert(myURL);27 动态输出HTMLdocument.write(Heres some information about this document:);document.write();document.write(Referring Document: + document.referrer + );document.write(Domain: + document.domain + );document.write(URL: + document.URL + );document.write();28 输出换行document.writeln(a);document.writeln(b);29 输出日期var thisDate = new Date();document.write(thisDate.toString();30 指定日期的时区var myOffset = -2;var currentDate = new Date();var userOffset = currentDate.getTimezoneOffset()/60;var timeZoneDifference = userOffset - myOffset;currentDate.setHours(currentDate.getHours() + timeZoneDifference);document.write(The time and date in Central Europe is: + currentDate.toLocaleString();31 设置日期输出格式var thisDate = new Date();var thisTimeString = thisDate.getHours() + : + thisDate.getMinutes();var thisDateString = thisDate.getFullYear() + / + thisDate.getMonth() + / + thisDate.getDate();document.write(thisTimeString + on + thisDateString);32 读取URL参数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 );你还以为HTML是无状态的么?33 打开一个新的document对象function newDocument() document.open();document.write(This is a New Document.);document.close();34 页面跳转window.location = ;35 添加网页加载进度窗口var placeHolder = window.open(,placeholder,width=200,height=200);The Main PageThis is the main page36 读取图像属性Width37 动态加载图像myImage = new Image;myImage.src = /images/logo.jpg;38 简单的图像替换rollImage = new Image;rollImage.src = /images/logo.jpg;defaultImage = new Image;defaultImage.src = image1.jpg;39 随机显示图像var imageList = new Array;imageList0 = image1.jpg;imageList1 = image2.jpg;imageList2 = image3.jpg;imageList3 = image4.jpg;var imageChoice = Math.floor(Math.random() * imageList.length);document.write();40 函数实现的图像替换var source = 0;var replacement = 1;function createRollOver(originalImage,replacementImage) var imageArray = new Array;imageArraysource = new Image;imageArraysource.src = originalImage;imageArrayreplacement = new Image;imageArrayreplacement.src = replacementImage;return imageArray;1var rollImage1 = createRollOver(image1.jpg,rollImage1.jpg);41 创建幻灯片var imageList = new Array;imageList0 = new Image;imageList0.src = image1.jpg;imageList1 = new Image;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(slideShow( + imageNumber + ),3000);42 随机广告图片var imageList = new Array;imageList0 = image1.jpg;imageList1 = image2.jpg;imageList2 = image3.jpg;imageList3 = image4.jpg;var urlList = new Array;urlList0 = /;urlList1 = /;urlList2 = /;urlList3 = /;var imageChoice = Math.floor(Math.random() * imageList.length);document.write();43 表单构成First ChoiceSecond Choice44 访问表单中的文本框内容Check Text Field45 动态复制文本框内容Enter some Text: Copy Text: Copy Text Field46 侦测文本框的变化Enter some Text: 47 访问选中的Select123Check Selection List48 动态增加Select项12document.myForm.mySelect.length+;document.myForm.mySelect.optionsdocument.myForm.mySelect.length - 1.text = 3;document.myForm.mySelect.optionsdocument.myForm.mySelect.length - 1.value = Third Choice;49 验证表单字段function checkField(field) if (field.value = ) window.alert(You must enter a value in the field);field.focus();Text Field: 50 验证Select项function checkList(selection) if (selection.length = 0) window.alert(You must make a selection from the list.);return false;return true;51 动态改变表单的actionUsername: Password: 52 使用图像按钮Username: Password: 53 表单数据的加密!-function encrypt(item) var newItem = ;for (i=0; i item.length; i+) newItem += item.charCodeAt(i) + .;return newItem;function encryptForm(myForm) for (i=0; i Enter Some Text: 20: JavaScript就这么回事5:窗口和框架54 改变浏览器状态栏文字提示window.status = A new status message;55 弹出确认提示框var userChoice = window.confirm(Click OK or Cancel);if (userChoice) document.write(You chose OK); else document.write(You chose Cancel);56 提示输入var userName = mpt(Please Enter Your Name,Enter Your Name Here);document.write(Your Name is + userName);57 打开一个新窗口/打开一个名称为myNewWindow的浏览器新窗口window.open(/,myNewWindow);58 设置新窗口的大小window.open(/,myNewWindow,height=300,width=300);59 设置新窗口的位置window.open(/,myNewWindow,height=300,width=300,left=200,screenX=200,top=100,screenY=100);60 是否显示工具栏和滚动栏window.open(http:61 是否可以缩放新窗口的大小window.open(/ , myNewWindow, resizable=yes );62 加载一个新的文档到当前窗口Open New Document63 设置页面的滚动位置if (document.all) /如果是IE浏览器则使用scrollTop属性document.body.scrollTop = 200; else /如果是NetScape浏览器则使用pageYOffset属性window.pageYOffset = 200

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论