适合阅读范围:对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:
2: hello to the non-javascript browser. 3: 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'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('holder.html','placeholder','width=200,height=200'); <br/>5: </script>
6:
The Main Page
7:
8:
9:
This is the main page
10:
11:
JavaScript就这么回事3:图像
36 读取图像属性
1:
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:
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=”' + imageList[imageChoice] + ‘“ alt="整理的比较不错的JavaScript的方法和技巧_基础知识" >'); <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:
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:
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=”' + urlList[imageChoice] + ‘“><img src=”' + imageList[imageChoice] + ‘“ alt="整理的比较不错的JavaScript的方法和技巧_基础知识" >'); <br/>14: </script>
JavaScript就这么回事4:表单
还是先继续写完JS就这么回事系列吧~
43 表单构成
1:
44 访问表单中的文本框内容
1:
4:
Check Text Field 45 动态复制文本框内容
1:
5:
6: document.myForm.myText.value;”>Copy Text Field
46 侦测文本框的变化
1:
47 访问选中的Select
1:
8:
Check Selection List 48 动态增加Select项
1:
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:
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:
52 使用图像按钮
1:
6:
53 表单数据的加密
1: <script> <br/>2: <!-- <br/>3: function encrypt(item) { <br/>4: var newItem = ''; <br/>5: for (i=0; i < item.length; i++) { <br/>6: newItem += item.charCodeAt(i) + '.'; <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:
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”,'height=300,width=300'); <br/>3: </script>
59 设置新窗口的位置
1: <script> <br/>2: window.open(“http://www.php.cn/”,”myNewWindow”,'height=300,width=300,left=200,screenX=200,top=100,screenY=100'); <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('http://www.liu21st.com/' , 'myNewWindow', 'resizable=yes' );</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: