




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/Js基础代码1创建脚本块引用内容程序代码
<scriptlanguage=”JavaScript”>
JavaScript代码写在这里面
</script>2隐藏脚本代码
引用内容程序代码
<scriptlanguage=”JavaScript”>
<!–
document.write(“Hello”);
//–>
</script>
在不支持JavaScript的浏览器中将不执行相关代码3浏览器不支持的时候显示
引用内容程序代码
<noscript>
Hellotothenon-JavaScriptbrowser.
</noscript>4链接外部脚本文件
引用内容程序代码
<scriptlanguage=”JavaScript”src=”/”””></script>5注释脚本
引用内容程序代码
//Thisisacomment
document.write(“Hello”);//Thisisacomment
/*
Allofthis
isacomment
*/6输出到浏览器
引用内容程序代码
document.write(“<strong>Hello</strong>”);7定义变量
引用内容程序代码
varmyVariable=“somevalue”;8字符串相加
引用内容程序代码
varmyString=“String1”+“String2”;9字符串搜索
引用内容程序代码
<scriptlanguage=”JavaScript”>
<!–
varmyVariable=“Hellothere”;
vartherePlace=myVariable.search(“there”);
document.write(therePlace);
//–>
</script>10字符串替换
引用内容程序代码
thisVar.replace(“Monday”,”Friday”);11格式化字串
引用内容程序代码
<scriptlanguage=”JavaScript”>
<!–
varmyVariable=“Hellothere”;
document.write(myVariable.big()+“<br>”);
document.write(myVariable.blink()+“<br>”);
document.write(myVariable.bold()+“<br>”);
document.write(myVariable.fixed()+“<br>”);
document.write(myVariable.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>”);
document.write(myVariable.sup()+“<br>”);
document.write(myVariable.toLowerCase()+“<br>”);
document.write(myVariable.toUpperCase()+“<br>”);
varfirstString=“MyString”;
varfinalString=firstString.bold().toLowerCase().fontcolor(“red”);
//–>
</script>12创建数组
引用内容程序代码
<scriptlanguage=”JavaScript”>
<!–
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”);
//–>
</script>13数组排序
引用内容程序代码
<scriptlanguage=”JavaScript”>
<!–
varmyArray=newArray(5);
myArray[0]=“z”;
myArray[1]=“c”;
myArray[2]=“d”;
myArray[3]=“a”;
myArray[4]=“q”;
document.write(myArray.sort());
//–>
</script>14分割字符串
引用内容程序代码
<scriptlanguage=”JavaScript”>
<!–
varmyVariable=“a,b,c,d”;
varstringArray=myVariable.split(“,”);
document.write(stringArray[0]);
document.write(stringArray[1]);
document.write(stringArray[2]);
document.write(stringArray[3]);
//–>
</script>15弹出警告信息
引用内容程序代码
<scriptlanguage=”JavaScript”>
<!–
window.alert(“Hello”);
//–>
</script>16弹出确认框
引用内容程序代码
<scriptlanguage=”JavaScript”>
<!–
varresult=window.confirm(“ClickOKtocontinue”);
//–>
</script>17自定义函数
引用内容程序代码
<scriptlanguage=”JavaScript”>
<!–
functionmultiple(number1,number2){
varresult=number1*number2;
returnresult;
}
//–>
</script>18调用JS函数
引用内容程序代码
<ahref=”#”onClick=”functionName()”>Linktext</a>
<ahref=”/”javascript:functionName”()”>Linktext</a>19在页面加载完成后执行函数
引用内容程序代码
<bodyonLoad=”functionName();”>
Bodyofthepage
</body>20条件判断
引用内容程序代码
<script>
<!–
varuserChoice=window.confirm(“ChooseOKorCancel”);
varresult=(userChoice==true)?“OK”:“Cancel”;
document.write(result);
//–>
</script>21指定次数循环
引用内容程序代码
<script>
<!–
varmyArray=newArray(3);
myArray[0]=“Item0”;
myArray[1]=“Item1”;
myArray[2]=“Item2”;
for(i=0;i<myArray.length;i++){
document.write(myArray[i]+“<br>”);
}
//–>
</script>22设定将来执行
引用内容程序代码
<script>
<!–
functionhello(){
window.alert(“Hello”);
}
window.setTimeout(“hello()”,5000);
//–>
</script>23定时执行函数
引用内容程序代码
<script>
<!–
functionhello(){
window.alert(“Hello”);
window.setTimeout(“hello()”,5000);
}
window.setTimeout(“hello()”,5000);
//–>
</script>24取消定时执行
引用内容程序代码
<script>
<!–
functionhello(){
window.alert(“Hello”);
}
varmyTimeout=window.setTimeout(“hello()”,5000);
window.clearTimeout(myTimeout);
//–>
</script>25在页面卸载时候执行函数
引用内容程序代码
<bodyonUnload=”functionName();”>
Bodyofthepage
</body>JavaScript就这么回事2:浏览器输出26访问document对象
引用内容程序代码
<scriptlanguage=”JavaScript”>
varmyURL=document.URL;
window.alert(myURL);
</script>27动态输出HTML
引用内容程序代码
<scriptlanguage=”JavaScript”>
document.write(“<p>Here’ssomeinformationaboutthisdocument:</p>”);
document.write(“<ul>”);
document.write(“<li>ReferringDocument:“+document.referrer+“</li>”);
document.write(“<li>Domain:“+document.domain+“</li>”);
document.write(“<li>URL:“+document.URL+“</li>”);
document.write(“</ul>”);
</script>28输出换行
引用内容程序代码
document.writeln(“<strong>a</strong>”);
document.writeln(“b”);29输出日期
引用内容程序代码
<scriptlanguage=”JavaScript”>
varthisDate=newDate();
document.write(thisDate.toString());
</script>30指定日期的时区
引用内容程序代码
<scriptlanguage=”JavaScript”>
varmyOffset=-2;
varcurrentDate=newDate();
varuserOffset=currentDate.getTimezoneOffset()/60;
vartimeZoneDifference=userOffset–myOffset;
currentDate.setHours(currentDate.getHours()+timeZoneDifference);
document.write(“ThetimeanddateinCentralEuropeis:“+currentDate.toLocaleString());
</script>31设置日期输出格式
引用内容程序代码
<scriptlanguage=”JavaScript”>
varthisDate=newDate();
varthisTimeString=thisDate.getHours()+“:”+thisDate.getMinutes();
varthisDateString=thisDate.getFullYear()+“/”+thisDate.getMonth()+“/”+thisDate.getDate();
document.write(thisTimeString+“on“+thisDateString);
</script>32读取URL参数
引用内容程序代码
<scriptlanguage=”JavaScript”>
varurlParts=document.URL.split(“?”);
varparameterParts=urlParts[1].split(“&”);
for(i=0;i<parameterParts.length;i++){
varpairParts=parameterParts[i].split(“=”);
varpairName=pairParts[0];
varpairValue=pairParts[1];
document.write(pairName+“:“+pairValue);
}
</script>
你还以为HTML是无状态的么?33打开一个新的document对象
引用内容程序代码
<scriptlanguage=”JavaScript”>
functionnewDocument(){
document.open();
document.write(“<p>ThisisaNewDocument.</p>”);
document.close();
}
</script>34页面跳转
引用内容程序代码
<scriptlanguage=”JavaScript”>
window.location=“”;
</script>35添加网页加载进度窗口
引用内容程序代码
<html>
<head>
<scriptlanguage=’javaScript’>
varplaceHolder=window.open(‘holder.html’,'placeholder’,'width=200,height=200′);
</script>
<title>TheMainPage</title>
</head>
<bodyonLoad=’placeHolder.close()’>
<p>Thisisthemainpage</p>
</body>
</html>JavaScript就这么回事3:图像36读取图像属性
引用内容程序代码
<imgsrc=”/”image1.jpg””name=”myImage”>
<ahref=”#”onClick=”window.alert(document.myImage.width)”>Width</a>37动态加载图像
引用内容程序代码
<scriptlanguage=”JavaScript”>
myImage=newImage;
myImage.src=“Tellers1.jpg”;
</script>38简单的图像替换
引用内容程序代码
<scriptlanguage=”JavaScript”>
rollImage=newImage;
rollImage.src=“rollImage1.jpg”;
defaultImage=newImage;
defaultImage.src=“image1.jpg”;
</script>
<ahref=”/”myUrl””onMouseOver=”document.myImage.src=rollImage.src;”
onMouseOut=”document.myImage.src=defaultImage.src;”>
<imgsrc=”/”image1.jpg””name=”myImage”width=100height=100border=0>39随机显示图像
引用内容程序代码
<scriptlanguage=”JavaScript”>
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]+‘“>’);
</script>40函数实现的图像替换
引用内容程序代码
<scriptlanguage=”JavaScript”>
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”);
</script>
<ahref=”#”onMouseOver=”document.myImage1.src=rollImage1[replacement].src;”
onMouseOut=”document.myImage1.src=rollImage1[source].src;”>
<imgsrc=”/”image1.jpg””width=100name=”myImage1”border=0>
</a>41创建幻灯片
引用内容程序代码
<scriptlanguage=”JavaScript”>
varimageList=newArray;
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);
}
}
</script>
</head>
<bodyonLoad=”slideShow(0)”>
<imgsrc=”/”image1.jpg””width=100name=”slideShow”>42随机广告图片
引用内容程序代码
<scriptlanguage=”JavaScript”>
varimageList=newArray;
imageList[0]=“image1.jpg”;
imageList[1]=“image2.jpg”;
imageList[2]=“image3.jpg”;
imageList[3]=“image4.jpg”;
varurlList=newArray;
urlList[0]=“”;
urlList[1]=“”;
urlList[2]=“”;
urlList[3]=“”;
varimageChoice=Math.floor(Math.random()*imageList.length);
document.write(‘<ahref=”’+urlList[imageChoice]+‘“><imgsrc=”’+imageList[imageChoice]+‘“></a>’);
</script>JavaScript就这么回事4:表单43表单构成
引用内容程序代码
<formmethod=”post”action=”target.html”name=”thisForm”>
<inputtype=”text”name=”myText”>
<selectname=”mySelect”>
<optionvalue=”1”>FirstChoice</option>
<optionvalue=”2”>SecondChoice</option>
</select>
<br>
<inputtype=”submit”value=”SubmitMe”>
</form>44访问表单中的文本框内容
引用内容程序代码
<formname=”myForm”>
<inputtype=”text”name=”myText”>
</form>
<ahref=’#’onClick=’window.alert(document.myForm.myText.value);’>CheckTextField</a>45动态复制文本框内容
引用内容程序代码
<formname=”myForm”>
EntersomeText:<inputtype=”text”name=”myText”><br>
CopyText:<inputtype=”text”name=”copyText”>
</form>
<ahref=”#”onClick=”document.myForm.copyText.value=
document.myForm.myText.value;”>CopyTextField</a>46侦测文本框的变化
引用内容程序代码
<formname=”myForm”>
EntersomeText:<inputtype=”text”name=”myText”onChange=”alert(this.value);”>
</form>47访问选中的Select
引用内容程序代码
<formname=”myForm”>
<selectname=”mySelect”>
<optionvalue=”FirstChoice”>1</option>
<optionvalue=”SecondChoice”>2</option>
<optionvalue=”ThirdChoice”>3</option>
</select>
</form>
<ahref=’#’onClick=’alert(document.myForm.mySelect.value);’>CheckSelectionList</a>48动态增加Select项
引用内容程序代码
<formname=”myForm”>
<selectname=”mySelect”>
<optionvalue=”FirstChoice”>1</option>
<optionvalue=”SecondChoice”>2</option>
</select>
</form>
<scriptlanguage=”JavaScript”>
document.myForm.mySelect.length++;
document.myForm.mySelect.options[document.myForm.mySelect.length-1].text=“3”;
document.myForm.mySelect.options[document.myForm.mySelect.length-1].value=“ThirdChoice”;
</script>49验证表单字段
引用内容程序代码
<scriptlanguage=”JavaScript”>
functioncheckField(field){
if(field.value==“”){
window.alert(“Youmustenteravalueinthefield”);
field.focus();
}
}
</script>
<formname=”myForm”action=”target.html”>
TextField:<inputtype=”text”name=”myField”onBlur=”checkField(this)”>
<br><inputtype=”submit”>
</form>50验证Select项
引用内容程序代码
functioncheckList(selection){
if(selection.length==0){
window.alert(“Youmustmakeaselectionfromthelist.”);
returnfalse;
}
returntrue;
}51动态改变表单的action
引用内容程序代码
<formname=”myForm”action=”login.html”>
Username:<inputtype=”text”name=”username”><br>
Password:<inputtype=”password”name=”password”><br>
<inputtype=”button”value=”Login”onClick=”this.form.submit();”>
<inputtype=”button”value=”Register”onClick=”this.form.action=‘register.html’;this.form.submit();”>
<inputtype=”button”value=”RetrievePassword”onClick=”this.form.action=‘password.html’;this.form.submit();”>
</form>52使用图像按钮
引用内容程序代码
<formname=”myForm”action=”login.html”>
Username:<inputtype=”text”name=”username”><br>
Password:<inputtype=”password”name=”password”><br>
<inputtype=”image”src=”/”login.gif””value=”Login”>
</form>53表单数据的加密
引用内容程序代码
<SCRIPTLANGUAGE=’JavaScript’>
<!–
functionencrypt(item){
varnewItem=”;
for(i=0;i<item.length;i++){
newItem+=item.charCodeAt(i)+‘.’;
}
returnnewItem;
}
functionencryptForm(myForm){
for(i=0;i<myForm.elements.length;i++){
myForm.elements[i].value=encrypt(myForm.elements[i].value);
}
}//–>
</SCRIPT>
<formname=’myForm’onSubmit=’encryptForm(this);window.alert(this.myField.value);’>
EnterSomeText:<inputtype=textname=myField><inputtype=submit>
</form>JavaScript就这么回事5:窗口和框架54改变浏览器状态栏文字提示
引用内容程序代码
<scriptlanguage=”JavaScript”>
window.status=“Anewstatusmessage”;
</script>55弹出确认提示框
引用内容程序代码
<scriptlanguage=”JavaScript”>
varuserChoice=window.confirm(“ClickOKorCancel”);
if(userChoice){
document.write(“YouchoseOK”);
}else{
document.write(“YouchoseCancel”);
}
</script>56提示输入
引用内容程序代码
<scriptlanguage=”JavaScript”>
varuserName=mpt(“PleaseEnterYourName”,”EnterYourNameHere”);
document.write(“YourNameis“+userName);
</script>57打开一个新窗口
引用内容//打开一个名称为myNewWindow的浏览器新窗口
程序代码
<scriptlanguage=”JavaScript”>
window.open(“”,”myNewWindow”);
</script>58设置新窗口的大小
引用内容程序代码
<scriptlanguage=”JavaScript”>
window.open(“”,”myNewWindow”,’height=300,width=300′);
</script>59设置新窗口的位置
引用内容程序代码
<scriptlanguage=”JavaScript”>
window.open(“”,”myNewWindow”,’height=300,width=300,left=200,screenX=200,top=100,screenY=100′);
</script>60是否显示工具栏和滚动栏
引用内容程序代码
<scriptlanguage=”JavaScript”>
window.open(“”,toolbar=no,menubar=no);
</script>61是否可以缩放新窗口的大小
引用内容程序代码
<scriptlanguage=”JavaScript”>
window.open(‘’,‘myNewWindow’,‘resizable=yes’);</script>62加载一个新的文档到当前窗口
引用内容程序代码
<ahref=’#’onClick=’document.location=‘125a.html’;’>OpenNewDocument</a>63设置页面的滚动位置
引用内容程序代码
<scriptlanguage=”JavaScript”>
if(document.all){//如果是IE浏览器则使用scrollTop属性
document.body.scrollTop=200;
}else{//如果是NetScape浏
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年财务报告要求试题及答案
- 泡沫塑料应用领域考核试卷
- 2025年金属非金属矿井通风证考试题及答案
- 项目管理质量控制问题及答案
- 2023年中国邮政集团有限公司湖北省分公司社会招聘【186岗】笔试参考题库附带答案详解
- 2024年项目管理资格认证的实践试题及答案
- 种下希望种子2025年国际金融理财师考试试题及答案
- 乐器清洁保护方案范本
- 皮装饰制品的个性化定制服务考核试卷
- 纳米尺度电子器件的量子效应研究考核试卷
- 统编版2024-2025学年语文六年级下册期中核心素养评估卷有答案
- 2025-2030中国浮吊行业市场发展趋势与前景展望战略分析研究报告
- 918勿忘国耻铭记历史课件-高一上学期主题班会
- 船舶英语考试练习题及答案1-2023-练习版
- 2025年中国景泰蓝摆件盒市场调查研究报告
- 公共关系学电子教案
- 王者荣耀考试试题及答案
- 杭州市市级机关事业单位招聘真题2024
- 高速公路消防知识
- 地下混凝土水池蓄水试验方案20240401
- 头晕、抑郁与焦虑关系解析与应对策略
评论
0/150
提交评论