首页 > web前端 > js教程 > 正文

JS入门代码集合_基础知识

PHP中文网
发布: 2016-05-16 19:04:24
原创
886人浏览过

在闪吧看见了一篇js教程,感觉不错 适合阅读范围:对javascript一无所知~离精通只差一步之遥的人

 基础知识:HTML 
JavaScript就这么回事1:基础知识   

1 创建脚本块  

1: <script> <br/>2: JavaScript code goes here <br/>3: </script>   
2 隐藏脚本代码  
1: <script> <br/>2: <!-- <br/>3: document.write(“Hello”); <br/>4: // --> <br/>5: </script>   
在不支持JavaScript的浏览器中将不执行相关代码  
3 浏览器不支持的时候显示  
1: 

   

4 链接外部脚本文件  
1: <script></script>   

5 注释脚本  
1: // This is a comment  
2: document.write(“Hello”); // This is a comment  
3: /*  
4: All of 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: <script> <br/>2: <!-- <br/>3: var myVariable = “Hello there”; <br/>4: var therePlace = myVariable.search(“there”); <br/>5: document.write(therePlace); <br/>6: // --> <br/>7: </script>   

10 字符串替换  

1: thisVar.replace(“Monday”,”Friday”);   

11 格式化字串  

1: <script> <br/>2: <!-- <br/>3: var myVariable = “Hello there”; <br/>4: document.write(myVariable.big() + “<br>”); <br/>5: document.write(myVariable.blink() + “<br>”); <br/>6: document.write(myVariable.bold() + “<br>”); <br/>7: document.write(myVariable.fixed() + “<br>”); <br/>8: document.write(myVariable.fontcolor(“red”) + “<br>”); <br/>9: document.write(myVariable.fontsize(“18pt”) + “<br>”); <br/>10: document.write(myVariable.italics() + “<br>”); <br/>11: document.write(myVariable.small() + “<br>”); <br/>12: document.write(myVariable.strike() + “<br>”); <br/>13: document.write(myVariable.sub() + “<br>”); <br/>14: document.write(myVariable.sup() + “<br>”); <br/>15: document.write(myVariable.toLowerCase() + “<br>”); <br/>16: document.write(myVariable.toUpperCase() + “<br>”); <br/>17: <br/>18: var firstString = “My String”; <br/>19: var finalString = firstString.bold().toLowerCase().fontcolor(“red”); <br/>20: // --> <br/>21: </script>   

12 创建数组  
1: <script> <br/>2: <!-- <br/>3: var myArray = new Array(5); <br/>4: myArray[0] = “First Entry”; <br/>5: myArray[1] = “Second Entry”; <br/>6: myArray[2] = “Third Entry”; <br/>7: myArray[3] = “Fourth Entry”; <br/>8: myArray[4] = “Fifth Entry”; <br/>9: var anotherArray = new Array(“First Entry”,”Second Entry”,”Third Entry”,”Fourth Entry”,”Fifth Entry”); <br/>10: // --> <br/>11: </script>   

13 数组排序  
1: <script> <br/>2: <!-- <br/>3: var myArray = new Array(5); <br/>4: myArray[0] = “z”; <br/>5: myArray[1] = “c”; <br/>6: myArray[2] = “d”; <br/>7: myArray[3] = “a”; <br/>8: myArray[4] = “q”; <br/>9: document.write(myArray.sort()); <br/>10: // --> <br/>11: </script>   



14 分割字符串  

1: <script> <br/>2: <!-- <br/>3: var myVariable = “a,b,c,d”; <br/>4: var stringArray = myVariable.split(“,”); <br/>5: document.write(stringArray[0]); <br/>6: document.write(stringArray[1]); <br/>7: document.write(stringArray[2]); <br/>8: document.write(stringArray[3]); <br/>9: // --> <br/>10: </script>   



15 弹出警告信息  

1: <script> <br/>2: <!-- <br/>3: window.alert(“Hello”); <br/>4: // --> <br/>5: </script>   



16 弹出确认框  

1: <script> <br/>2: <!-- <br/>3: var result = window.confirm(“Click OK to continue”); <br/>4: // --> <br/>5: </script>   



17 定义函数  

1: <script> <br/>2: <!-- <br/>3: function multiple(number1,number2) { <br/>4: var result = number1 * number2; <br/>5: return result; <br/>6: } <br/>7: // --> <br/>8: </script>   



18 调用JS函数  

1: Link text  
2: Link text   



19 在页面加载完成后执行函数  

1:   
2: Body of the page  
3:    


20 条件判断  

1: <script> <br/>2: <!-- <br/>3: var userChoice = window.confirm(“Choose OK or Cancel”); <br/>4: var result = (userChoice == true) ? “OK” : “Cancel”; <br/>5: document.write(result); <br/>6: // --> <br/>7: </script>   



21 指定次数循环  

1: <script> <br/>2: <!-- <br/>3: var myArray = new Array(3); <br/>4: myArray[0] = “Item 0”; <br/>5: myArray[1] = “Item 1”; <br/>6: myArray[2] = “Item 2”; <br/>7: for (i = 0; i < myArray.length; i++) { <br/>8: document.write(myArray[i] + “<br>”); <br/>9: } <br/>10: // --> <br/>11: </script>   



22 设定将来执行  

1: <script> <br/>2: <!-- <br/>3: function hello() { <br/>4: window.alert(“Hello”); <br/>5: } <br/>6: window.setTimeout(“hello()”,5000); <br/>7: // --> <br/>8: </script>   



23 定时执行函数  

1: <script> <br/>2: <!-- <br/>3: function hello() { <br/>4: window.alert(“Hello”); <br/>5: window.setTimeout(“hello()”,5000); <br/>6: } <br/>7: window.setTimeout(“hello()”,5000); <br/>8: // --> <br/>9: </script>   



24 取消定时执行  

1: <script> <br/>2: <!-- <br/>3: function hello() { <br/>4: window.alert(“Hello”); <br/>5: } <br/>6: var myTimeout = window.setTimeout(“hello()”,5000); <br/>7: window.clearTimeout(myTimeout); <br/>8: // --> <br/>9: </script>   



25 在页面卸载时候执行函数  

1:   
2: Body of the page  
3:    

JavaScript就这么回事2:浏览器输出   


26 访问document对象  

1: <script> <br/>2: var myURL = document.URL; <br/>3: window.alert(myURL); <br/>4: </script>   



27 动态输出HTML  

1: <script> <br/>2: document.write(“<p>Here&#39;s some information about this document:”); <br/>3: document.write(“<ul>”); <br/>4: document.write(“<li>Referring Document: “ + document.referrer + “”); <br/>5: document.write(“<li>Domain: “ + document.domain + “”); <br/>6: document.write(“<li>URL: “ + document.URL + “”); <br/>7: document.write(“”); <br/>8: </script>   


28 输出换行  

1: document.writeln(“a”);  
2: document.writeln(“b”);   



29 输出日期  

1: <script> <br/>2: var thisDate = new Date(); <br/>3: document.write(thisDate.toString()); <br/>4: </script>   



30 指定日期的时区  

1: <script> <br/>2: var myOffset = -2; <br/>3: var currentDate = new Date(); <br/>4: var userOffset = currentDate.getTimezoneOffset()/60; <br/>5: var timeZoneDifference = userOffset - myOffset; <br/>6: currentDate.setHours(currentDate.getHours() + timeZoneDifference); <br/>7: document.write(“The time and date in Central Europe is: “ + currentDate.toLocaleString()); <br/>8: </script>   


31 设置日期输出格式  

1: <script> <br/>2: var thisDate = new Date(); <br/>3: var thisTimeString = thisDate.getHours() + “:” + thisDate.getMinutes(); <br/>4: var thisDateString = thisDate.getFullYear() + “/” + thisDate.getMonth() + “/” + thisDate.getDate(); <br/>5: document.write(thisTimeString + “ on “ + thisDateString); <br/>6: </script>   


32 读取URL参数  

1: <script> <br/>2: var urlParts = document.URL.split(“?”); <br/>3: var parameterParts = urlParts[1].split(“&”); <br/>4: for (i = 0; i < parameterParts.length; i++) { <br/>5: var pairParts = parameterParts[i].split(“=”); <br/>6: var pairName = pairParts[0]; <br/>7: var pairValue = pairParts[1]; <br/>8: document.write(pairName + “ :“ +pairValue ); <br/>9: } <br/>10: </script>   

你还以为HTML是无状态的么?  

33 打开一个新的document对象  

1: <script> <br/>2: function newDocument() { <br/>3: document.open(); <br/>4: document.write(“<p>This is a New Document.”); <br/>5: document.close(); <br/>6: } <br/>7: </script>   



34 页面跳转  

1: <script> <br/>2: window.location = “http://www.php.cn/”; <br/>3: </script>   



35 添加网页加载进度窗口  

1:   
2:   
3: <script> <br/>4: var placeHolder = window.open(&#39;holder.html&#39;,&#39;placeholder&#39;,&#39;width=200,height=200&#39;); <br/>5: </script>  
6: The Main Page  
7:   
8:   
9: 

This is the main page

  
10:   
11:    



JavaScript就这么回事3:图像   



36 读取图像属性  

1: JS入门代码集合_基础知识  
2: Width  
3:   


37 动态加载图像  

1: <script> <br/>2: myImage = new Image; <br/>3: myImage.src = “Tellers1.jpg”; <br/>4: </script>   


38 简单的图像替换  

1: <script> <br/>2: rollImage = new Image; <br/>3: rollImage.src = “rollImage1.jpg”; <br/>4: defaultImage = new Image; <br/>5: defaultImage.src = “image1.jpg”; <br/>6: </script>  
7: 8: onMouseOut=”document.myImage.src = defaultImage.src;”>  
9: JS入门代码集合_基础知识   


39 随机显示图像  

1: <script> <br/>2: var imageList = new Array; <br/>3: imageList[0] = “image1.jpg”; <br/>4: imageList[1] = “image2.jpg”; <br/>5: imageList[2] = “image3.jpg”; <br/>6: imageList[3] = “image4.jpg”; <br/>7: var imageChoice = Math.floor(Math.random() * imageList.length); <br/>8: document.write(‘<img src=”&#39; + imageList[imageChoice] + ‘“ alt="JS入门代码集合_基础知识" >&#39;); <br/>9: </script>   


40 函数实现的图像替换  

1: <script> <br/>2: var source = 0; <br/>3: var replacement = 1; <br/>4: function createRollOver(originalImage,replacementImage) { <br/>5: var imageArray = new Array; <br/>6: imageArray[source] = new Image; <br/>7: imageArray[source].src = originalImage; <br/>8: imageArray[replacement] = new Image; <br/>9: imageArray[replacement].src = replacementImage; <br/>10: return imageArray; <br/>11: } <br/>12: var rollImage1 = createRollOver(“image1.jpg”,”rollImage1.jpg”); <br/>13: </script>  
14: 15: onMouseOut=”document.myImage1.src = rollImage1[source].src;”>  
16: JS入门代码集合_基础知识  
17:    


41 创建幻灯片  

1: <script> <br/>2: var imageList = new Array; <br/>3: imageList[0] = new Image; <br/>4: imageList[0].src = “image1.jpg”; <br/>5: imageList[1] = new Image; <br/>6: imageList[1].src = “image2.jpg”; <br/>7: imageList[2] = new Image; <br/>8: imageList[2].src = “image3.jpg”; <br/>9: imageList[3] = new Image; <br/>10: imageList[3].src = “image4.jpg”; <br/>11: function slideShow(imageNumber) { <br/>12: document.slideShow.src = imageList[imageNumber].src; <br/>13: imageNumber += 1; <br/>14: if (imageNumber < imageList.length) { <br/>15: window.setTimeout(“slideShow(“ + imageNumber + “)”,3000); <br/>16: } <br/>17: } <br/>18: </script>  
19:   
20:   
21: JS入门代码集合_基础知识   


42 随机广告图片  

1: <script> <br/>2: var imageList = new Array; <br/>3: imageList[0] = “image1.jpg”; <br/>4: imageList[1] = “image2.jpg”; <br/>5: imageList[2] = “image3.jpg”; <br/>6: imageList[3] = “image4.jpg”; <br/>7: var urlList = new Array; <br/>8: urlList[0] = “http://www.php.cn/”; <br/>9: urlList[1] = “http://www.php.cn/”; <br/>10: urlList[2] = “http://www.php.cn/”; <br/>11: urlList[3] = “http://www.php.cn/”; <br/>12: var imageChoice = Math.floor(Math.random() * imageList.length); <br/>13: document.write(‘<a href=”&#39; + urlList[imageChoice] + ‘“><img src=”&#39; + imageList[imageChoice] + ‘“ alt="JS入门代码集合_基础知识" >&#39;); <br/>14: </script>   

JavaScript就这么回事4:表单   


还是先继续写完JS就这么回事系列吧~  
43 表单构成  

1: 
  
2:   
3:   
7: 
  
8:   
9: 
   


44 访问表单中的文本框内容  

1: 
  
2:   
3: 
  
4: Check Text Field   


45 动态复制文本框内容  

1: 
  
2: Enter some Text: 
  
3: Copy Text:   
4: 
  
5: 6: document.myForm.myText.value;”>Copy Text Field   


46 侦测文本框的变化  

1: 
  
2: Enter some Text:   
3: 
   


47 访问选中的Select  

1: 
  
2:   
7: 
  
8: Check Selection List   


48 动态增加Select项  

1: 
  
2:   
6: 
  
7: <script> <br/>8: document.myForm.mySelect.length++; <br/>9: document.myForm.mySelect.options[document.myForm.mySelect.length - 1].text = “3”; <br/>10: document.myForm.mySelect.options[document.myForm.mySelect.length - 1].value = “Third Choice”; <br/>11: </script>   


49 验证表单字段  

1: <script> <br/>2: function checkField(field) { <br/>3: if (field.value == “”) { <br/>4: window.alert(“You must enter a value in the field”); <br/>5: field.focus(); <br/>6: } <br/>7: } <br/>8: </script>  
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 动态改变表单的action  

1: 
  
2: Username: 
  
3: Password: 
  
4:   
5:   
6:   
7: 
   


52 使用图像按钮  

1: 
  
2: Username: 
  
3: Password: 
  
4:   
5: 
  
6:   


53 表单数据的加密  

1: <script> <br/>2: <!-- <br/>3: function encrypt(item) { <br/>4: var newItem = &#39;&#39;; <br/>5: for (i=0; i < item.length; i++) { <br/>6: newItem += item.charCodeAt(i) + &#39;.&#39;; <br/>7: } <br/>8: return newItem; <br/>9: } <br/>10: function encryptForm(myForm) { <br/>11: for (i=0; i < myForm.elements.length; i++) { <br/>12: myForm.elements[i].value = encrypt(myForm.elements[i].value); <br/>13: } <br/>14: } <br/>15: <br/>16: //--> <br/>17: </script>  
18: 
  
19: Enter Some Text:   
20: 
   




JavaScript就这么回事5:窗口和框架   


54 改变浏览器状态栏文字提示  

1: <script> <br/>2: window.status = “A new status message”; <br/>3: </script>   


55 弹出确认提示框  

1: <script> <br/>2: var userChoice = window.confirm(“Click OK or Cancel”); <br/>3: if (userChoice) { <br/>4: document.write(“You chose OK”); <br/>5: } else { <br/>6: document.write(“You chose Cancel”); <br/>7: } <br/>8: </script>   


56 提示输入  

1: <script> <br/>2: var userName = window.prompt(“Please Enter Your Name”,”Enter Your Name Here”); <br/>3: document.write(“Your Name is “ + userName); <br/>4: </script>   


57 打开一个新窗口  

1: //打开一个名称为myNewWindow的浏览器新窗口  
2: <script> <br/>3: window.open(“http://www.php.cn/”,”myNewWindow”); <br/>4: </script>   


58 设置新窗口的大小  

1: <script> <br/>2: window.open(“http://www.php.cn/”,”myNewWindow”,&#39;height=300,width=300&#39;); <br/>3: </script>   


59 设置新窗口的位置  

1: <script> <br/>2: window.open(“http://www.php.cn/”,”myNewWindow”,&#39;height=300,width=300,left=200,screenX=200,top=100,screenY=100&#39;); <br/>3: </script>   


60 是否显示工具栏和滚动栏  

1: <script> <br/>2: window.open(“http: <br/><br/><br/>61 是否可以缩放新窗口的大小 <br/><br/>1: <script language=”JavaScript”> <br/>2: window.open(&#39;http://www.liu21st.com/&#39; , &#39;myNewWindow&#39;, &#39;resizable=yes&#39; );</script>   


62 加载一个新的文档到当前窗口  

1: Open New Document   


63 设置页面的滚动位置  

1: <script> <br/>2: if (document.all) { //如果是IE浏览器则使用scrollTop属性 <br/>3: document.body.scrollTop = 200; <br/>4: } else { //如果是NetScape浏览器则使用pageYOffset属性 <br/>5: window.pageYOffset = 200; <br/>6: }</script>   


64 在IE中打开全屏窗口  

1: Open a full-screen window   


65 新窗口和父窗口的操作  

1: <script> <br/>2: //定义新窗口 <br/>3: var newWindow = window.open(“128a.html”,”newWindow”); <br/>4: newWindow.close(); //在父窗口中关闭打开的新窗口 <br/>5: </script>  
6: 在新窗口中关闭父窗口  
7: window.opener.close()   


66 往新窗口中写内容  

1: <script> <br/>2: var newWindow = window.open(“”,”newWindow”); <br/>3: newWindow.document.open(); <br/>4: newWindow.document.write(“This is a new window”); <br/>5: newWIndow.document.close(); <br/>6: </script>   


67 加载页面到框架页面  

1:   
2:   
3:   
4:   
5: 在frame1中加载frame2中的页面  
6: parent.frame2.document.location = “135b.html”;   


68 在框架页面之间共享脚本  
如果在frame1中html文件中有个脚本  

1: function doAlert() {   
2: window.alert(“Frame 1 is loaded”);  
3: }   

那么在frame2中可以如此调用该方法  

1:   
2: This is frame 2.  
3:    


69 数据公用  
可以在框架页面定义数据项,使得该数据可以被多个框架中的页面公用  

1: <script> <br/>2: var persistentVariable = “This is a persistent value”; <br/>3: </script>  
4:   
5:   
6:   
7:    

这样在frame1和frame2中都可以使用变量persistentVariable   
70 框架代码库  
根据以上的一些思路,我们可以使用一个隐藏的框架页面来作为整个框架集的代码库  

1:   
2:   
3:   
4:   
5: 
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号