javascript就那么回事Word文件下载.docx
《javascript就那么回事Word文件下载.docx》由会员分享,可在线阅读,更多相关《javascript就那么回事Word文件下载.docx(14页珍藏版)》请在冰豆网上搜索。
strong>
Hello<
/strong>
”);
7定义变量
varmyVariable=“somevalue”;
8字符串相加
varmyString=“String1”+“String2”;
9字符串搜索
varmyVariable=“Hellothere”;
vartherePlace=myVariable.search(“there”);
document.write(therePlace);
10字符串替换
thisVar.replace(“Monday”,”Friday”);
11格式化字串
document.write(myVariable.big()+“<
br>
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()+“<
varfirstString=“MyString”;
varfinalString=firstString.bold().toLowerCase().fontcolor(“red”);
12创建数组
varmyArray=newArray(5);
myArray[0]=“FirstEntry”;
myArray[1]=“SecondEntry”;
myArray[2]=“ThirdEntry”;
myArray[3]=“FourthEntry”;
myArray[4]=“FifthEntry”;
varanotherArray=newArray(“FirstEntry”,”SecondEntry”,”ThirdEntry”,”FourthEntry”,”FifthEntry”);
13数组排序
myArray[0]=“z”;
myArray[1]=“c”;
myArray[2]=“d”;
myArray[3]=“a”;
myArray[4]=“q”;
document.write(myArray.sort());
14分割字符串
varmyVariable=“a,b,c,d”;
varstringArray=myVariable.split(“,”);
document.write(stringArray[0]);
document.write(stringArray[1]);
document.write(stringArray[2]);
document.write(stringArray[3]);
15弹出警告信息
window.alert(“Hello”);
16弹出确认框
varresult=window.confirm(“ClickOKtocontinue”);
17自定义函数
functionmultiple(number1,number2){
varresult=number1*number2;
returnresult;
}
18调用JS函数
ahref=”#”onClick=”functionName()”>
Linktext<
/a>
ahref=”/”javascript:
functionName”()”>
19在页面加载完成后执行函数
bodyonLoad=”functionName();
”>
Bodyofthepage
/body>
20条件判断
script>
varuserChoice=window.confirm(“ChooseOKorCancel”);
varresult=(userChoice==true)?
“OK”:
“Cancel”;
document.write(result);
21指定次数循环
varmyArray=newArray(3);
myArray[0]=“Item0”;
myArray[1]=“Item1”;
myArray[2]=“Item2”;
for(i=0;
i<
myArray.length;
i++){
document.write(myArray[i]+“<
22设定将来执行
functionhello(){
window.setTimeout(“hello()”,5000);
23定时执行函数
24取消定时执行
varmyTimeout=window.setTimeout(“hello()”,5000);
window.clearTimeout(myTimeout);
25在页面卸载时候执行函数
bodyonUnload=”functionName();
JavaScript就这么回事2:
浏览器输出
26访问document对象
varmyURL=document.URL;
window.alert(myURL);
27动态输出HTML
p>
Here’ssomeinformationaboutthisdocument:
/p>
ul>
li>
ReferringDocument:
“+document.referrer+“<
/li>
Domain:
“+document.domain+“<
URL:
“+document.URL+“<
/ul>
28输出换行
document.writeln(“<
a<
document.writeln(“b”);
29输出日期
varthisDate=newDate();
document.write(thisDate.toString());
30指定日期的时区
varmyOffset=-2;
varcurrentDate=newDate();
varuserOffset=currentDate.getTimezoneOffset()/60;
vartimeZoneDifference=userOffset–myOffset;
currentDate.setHours(currentDate.getHours()+timeZoneDifference);
document.write(“ThetimeanddateinCentralEuropeis:
“+currentDate.toLocaleString());
31设置日期输出格式
varthisTimeString=thisDate.getHours()+“:
”+thisDate.getMinutes();
varthisDateString=thisDate.getFullYear()+“/”+thisDate.getMonth()+“/”+thisDate.getDate();
document.write(thisTimeString+“on“+thisDateString);
32读取URL参数
varurlParts=document.URL.split(“?
varparameterParts=urlParts[1].split(“&
parameterParts.length;
varpairParts=parameterParts[i].split(“=”);
varpairName=pairParts[0];
varpairValue=pairParts[1];
document.write(pairName+“:
“+pairValue);
你还以为HTML是无状态的么?
33打开一个新的document对象
functionnewDocument(){
document.open();
ThisisaNewDocument.<
document.close();
34页面跳转
window.location=“http:
//www.x-
35添加网页加载进度窗口
html>
head>
scriptlanguage=’javaScript’>
varplaceHolder=window.open(‘holder.html’,'
placeholder’,'
width=200,height=200′);
title>
TheMainPage<
/title>
/head>
bodyonLoad=’placeHolder.close()’>
Thisisthemainpage<
/html>
JavaScript就这么回事3:
图像
36读取图像属性
imgsrc=”/”image1.jpg””name=”myImage”>
ahref=”#”onClick=”window.alert(document.myImage.width)”>
Width<
37动态加载图像
myImage=newImage;
myImage.src=“Tellers1.jpg”;
38简单的图像替换
rollImage=newImage;
rollImage.src=“rollImage1.jpg”;
defaultImage=newImage;
defaultImage.src=“image1.jpg”;
ahref=”/”myUrl””onMouseOver=”document.myImage.src=rollImage.src;
”
onMouseOut=”document.myImage.src=defaultImage.src;
imgsrc=”/”image1.jpg””name=”myImage”width=100height=100border=0>
39随机显示图像
varimageList=newArray;
imageList[0]=“image1.jpg”;
imageList[1]=“image2.jpg”;
imageList[2]=“image3.jpg”;
imageList[3]=“image4.jpg”;
varimageChoice=Math.floor(Math.random()*imageList.length);
document.write(‘<
imgsrc=”’+imageList[imageChoice]+‘“>
’);
40函数实现的图像替换
varsource=0;
varreplacement=1;
functioncreateRollOver(originalImage,replacementImage){
varimageArray=newArray;
imageArray[source]=newImage;
imageArray[source].src=originalImage;
imageArray[replacement]=newImage;
imageArray[replacement].src=replacementImage;
returnimageArray;
varrollImage=createRollOver(“image1.jpg”,”rollImage1.jpg”);
ahref=”#”onMouseOver=”document.myImage1.src=rollImage1[replacement].src;
onMouseOut=”document.myImage1.src=rollImage1[source].src;
imgsrc=”/”image1.jpg””width=100name=”myImage1”border=0>
41创建幻灯片
imageList[0]=newImage;
imageList[0].src=“image1.jpg”;
imageList[1]=newImage;
imageList[1].src=“image2.jpg”;
imageList[2]=newImage;
imageList[2].src=“image3.jpg”;
imageList[3]=newImage;
imageList[3].src=“image4.jpg”;
functionslideShow(imageNumber){
document.slideShow.src=imageList[imageNumber].src;
imageNumber+=1;
if(imageNumber<
imageList.length){
window.setTimeout(“slideShow(“+imageNumber+“)”,3000);
bodyonLoad=”slideShow(0)”>
imgsrc=”/”image1.jpg””width=100name=”slideShow”>
42随机广告图片
varurlList=newArray;
urlList[0]=“http:
//some.host/”;
urlList[1]=“http:
//another.host/”;
urlList[2]=“http:
//somewhere.else/”;
urlList[3]=“http:
//right.here/”;
ahref=”’+urlList[imageChoice]+‘“>
JavaScript就这么回事4:
表单
43表单构成
formmethod=”post”action=”target.html”name=”thisForm”>
inputtype=”text”name=”myText”>
selectname=”mySelect”>
optionvalue=”1”>
FirstChoice<
/option>
optionvalue=”2”>
SecondChoice<
/select>
inputtype=”submit”value=”SubmitMe”>
/form>
44访问表单中的文本框内容
formname=”myForm”>
ahref=’#’onClick=’window.alert(document.myForm.myText.value);
’>
CheckTextField<
45动态复制文本框内容