您好,欢迎访问一九零五行业门户网

分享我学习js的过程 作者aircy javascript学习教程

前言:
      接触js以来,一直没有好好学完过一本js的书;从今天开始,我将从新开始学习、整理并分享我学习过程中的js代码。
今后发布的代码中并不代表全属于原创,相反很多部分都会源于互联网,当然也不会少于无忧脚本的。希望大家看了之后不要
在论坛训斥,毕竟“面斥不雅”!在这里发布出来,纯粹是为了共享给哪些曾经和我一样,或者正在学习前线的朋友们;同时我
也希望在这里得到更多人的支持,如果朋友有什么建议和意见,请多多跟帖。共同探讨!thanks!
实例一、
     本实例主要介绍了navigator、cookie、screen、location对象、函数调用以及prompt、alert、confirm交互的简单应用。
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <title> new document </title> <script language="javascript"> if(confirm("真的要看吗?")==true){ var password; password = prompt("密码520:","请输入密码吧!"); if(password=="520"){ alert("恭喜你了,进去吧!"); document.write("测试利用navigator对象检测浏览器如下信息: ") document.write("浏览器的名称:"+navigator.appname+" "); document.write("浏览器的版本号:"+navigator.appversion+" "); document.write("运行平台:"+navigator.platform+" "); document.write("是否支持cookie:"+navigator.cookieenabled+" "); document.write("测试利用screen对象获得浏览器窗口分辩率的大小: "); document.write("窗口高度:"+screen.height+" "); document.write("窗口宽度:"+screen.width+" "); document.write("颜色深度:"+screen.colordepth+" "); }else{ message(); } } function loadingmessage(param){ alert("不好意思哦!"+param+"密码不对哦!再来吧!"); return false; } function message(){ loadingmessage("哈啰") } </script> </head> <body> <a href="#" onclick="window.location='http://www.baidu.com';">点击我</a> </body> </html>
实例二、
本实例主要介绍了event对象和事件的简单应用。
<html> <head> <meta http-equiv="content-type" content="text/html; charset=gb2312"> <title>mouse</title> <script language="javascript"> function catchevent() { var eventsrcid = event.srcelement.id; var eventtype = event.type; alert(eventsrcid+"捕获到了"+eventtype+"事件"); } function getposition() { var posx = event.clientx; var posy = event.clienty; window.status = "鼠标的位置是("+posx+","+posy+")"; } function getkey() { textfield.value=event.keycode+","+string.fromcharcode(event.keycode); } </script> </head> <body onmousemove="getposition();" > 鼠标在文本框中按下:<input type="text" name="textfield" onmousedown="alert('鼠标在文本框中按下')"> 键盘按下:<input type="text" name="textfield" onkeydown="alert('键盘按下');"> event对象: <input type="text" name="textfield" id="text" onclick="catchevent();"> <input type="submit" name="submit" value="提交" id="button" onclick="catchevent();"> </body> </html>
实例三、
本实例主要介绍了数组和其slice()方法的使用
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <title> new document </title> <meta name="generator" content="editplus"> <meta name="author" content=""> <meta name="keywords" content=""> <meta name="description" content=""> </head> <body> 数组和其slice()方法的使用 <script language="javascript"> <!-- var firstarray = new array(); firstarray[0]="0"; firstarray[1]="1"; firstarray[2]="2"; firstarray[3]="3"; firstarray[4]="4"; for(var i=0 ; i<firstarray.length;i++){ document.write("firstarray["+i+"]="+i+" "); } var firstarray = firstarray.slice(0,3); document.write(firstarray); //--> </script> </body> </html>
实例四、
本实例主要介绍了对象和构造方法的使用
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <title> new document </title> <meta name="generator" content="editplus"> <meta name="author" content=""> <meta name="keywords" content=""> <meta name="description" content=""> </head> <script language="javascript"> <!-- var myobject = new object();//创建一个空对象 myobject[0]="0";//给对象放值 myobject[1]="1"; document.write("对象的使用"+myobject[0]); function person(name,age)//构造方法 { this.name=name; this.age=age; this.sayhello=function() { alert("我的名字是"+this.name+",我的年龄是"+this.age); } } //--> </script> <body> 构造方法的使用 <script language="javascript"> var person1 = new person("","21"); person1.sayhello(); </script> </body> </html>
实例五(4.1)、
本实例主要介绍了document对象的使用
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <title> new document </title> </head> <body> <img src="c:/documents and settings/administrator/桌面/13608.gif" width="170" height="1" border="0" alt=""> <script language="javascript"> <!-- document.write("文件地址:"+document.location+" ") document.write("文件标题:"+document.title+" "); document.write("图片路径:"+document.images[0].src+" "); document.write("文本颜色:"+document.fgcolor+" "); document.write("背景颜色:"+document.bgcolor+" "); //--> </script> </body> </html>
实例六(4.2)、
本实例主要介绍了document对象读取表单元素的使用
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <title> new document </title> <script language="javascript"> <!-- function firstsubmit(){ alert(document.form1.a01.value);//将表单名为a01的值显示出来 } function copyfirstsubmit(){ alert(document.form1.a01.value); document.form1.a02.value=document.form1.a01.value;//将表单名为a01的值给a02,取a01的值也可以使用、、、//document.form1.elements[0].value } //--> </script> </head> <body> <form name="firstform" method=post action=""> <input type="text" name="firsttext" value="哈啰"> </form> <form name="secondform" method=post action=""> <input type="text" name="secondtext" value="哈啰"> <input type="submit" name="hehe" value="哈哈"> </form> <script language="javascript"> <!-- var first = document.firstform.name; var second = document.secondform.name; //alert("第一个表单的名字:"+first);//读取表单元素,将注释去点即可 //alert("第二个表单的名字:"+second); //alert("第二个表单的按钮的name是:"+document.secondform.elements[1].name); //alert("第二个表单文本域的值:"+document.secondform.elements[0].value); //alert("第一个文本域:"+document.firstform.firsttext.value); //--> </script> <form name="form1" method=post action=""> a01<input type="text" name="a01"/><input type="button" name="01s" value="提交" onclick="firstsubmit()"/> a02<input type="text" name="a02"/><input type="button" name="02s" value="提交" onclick="copyfirstsubmit()"/>×在a01中输入值后再提交 </form> </body> </html>
实例七(4.3)、
本实例主要介绍了document对象读取表单元素的使用,一个注册验证代码
<html> <head> <title>用户注册</title> <meta http-equiv="content-type" content="text/html; charset=gb2312"><style type="text/css"> <!-- body { margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; } .style1 {color: #ffffff} --> </style> <script type="text/javascript"> function form_submit() { if(regform.usernumber.value=="") { alert("用户名不能为空!"); return false; } else if(regform.userpassword.value=="") { alert("密码不能为空!"); return false; } else if(regform.userpassword.value!=regform.reuserpassword.value) { alert("两次输入的密码不一致!"); return false; } return true; //regform.submit(); //不采用表单提交 } </script> </head> <body> <form id="register" name="regform" method="post" action=""> <table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#0099ff"> <tr> <td align="center" valign="middle" bgcolor="#ffffff">用户注册</td> </tr> <tr> <td align="center" valign="middle" bgcolor="#ffffff">用户账号: <input name="usernumber" type="text" id="usernumber" size="15"></td> </tr> <tr> <td align="center" valign="middle" bgcolor="#ffffff">用户密码: <input name="userpassword" type="text" id="userpassword" size="15"></td> </tr> <tr> <td align="center" valign="middle" bgcolor="#ffffff">确认密码: <input name="reuserpassword" type="text" id="reuserpassword" size="15"></td> </tr> <tr> <td align="center" valign="middle" bgcolor="#ffffff">电子邮箱: <input name="email" type="text" id="email" size="15"></td> </tr> <tr> <td align="center" valign="middle" bgcolor="#ffffff"><input type="button" name="submit" value="提交" onclick="form_submit()"></td> </tr> </table> </form> </body> </html>
实例八(4.4)、
本实例主要介绍了document对象读取表单元素的使用(单选按钮、复选按钮的使用)
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <title> new document </title> <meta name="generator" content="editplus"> <meta name="author" content=""> <meta name="keywords" content=""> <meta name="description" content=""> <script language="javascript"> <!-- function sel_coun(){ var country = document.form1.country.length; //得到radio个数 for(var i=0;i<country;i++){ if(form1.country[i].checked){ alert(form1.country[i].value); break; }else{ continue; } } } function sel_love(){ var country = document.form1.love.length; //得到checkbox个数 var love =""; //new array(); for(var i=0;i<country;i++){ if(form1.love[i].checked){ love+=form1.love[i].value+"、"; } } love = love.substring(0,love.lastindexof("、")); alert("你的爱好有:"+love) } //--> </script> </head> <body> <form name ="form1" method=post action=""> 单选应用 <input type="radio" name="country" value="中国" checked>中国 <input type="radio" name="country" value="法国">法国 <input type="radio" name="country" value="美国">美国 <input type="button" value="提交" onclick="sel_coun();"/> 复选应用 <input type="checkbox" name="love" value="打球">打球 <input type="checkbox" name="love" value="游泳">游泳 <input type="checkbox" name="love" value="看书">看书 <input type="checkbox" name="love" value="跳舞">跳舞 <input type="button" value="提交" onclick="sel_love();"/> </form> </body> </html>
实例九(4.5)、
本实例主要介绍了document对象读取表单元素的使用(下拉菜单、链接属性的使用)
<html> <head> <title>下拉菜单</title> <script language="javascript"> function display() { if(document.selectform.team.selectedindex==0) //判断是否进行了选择 { alert("您没有做选择!"); } else { var index = document.selectform.team.selectedindex; //读出当前选项的下标 alert("您选择的球队是:"+document.selectform.team.options[index].value); } } </script> </head> <body> <div align="center"> <form action="" method="post" name="selectform" id="selectform"> <table width="70%" border="0"> <tr> <td>请选择喜欢的球队:</td> </tr> <tr> <td><select name="team"> <option value="0">--未选择--</option> <option value="巴塞罗那">巴塞罗那</option> <option value="ac米兰">ac米兰</option> <option value="尤文图斯">尤文图斯</option> <option value="曼彻斯特联">曼彻斯特联</option> <option value="切尔西">切尔西</option> </select></td> </tr> <tr> <td><input name="look" type="button" id="look" value="单击查看" onclick="display()"></td> </tr> </table> <a href="http://www.baidu.com" name="baidu" target="_blank">有问题百度一下</a> <a href="http://www.google.com" name="google" target="_blank">google也可以</a> <script language="javascript"> document.write("第一个连接的信息: "); document.write("<b>href:</b>"+document.links[0].href+" "); document.write("<b>pathname:</b>"+document.links[0].pathname+" "); document.write("<b>port:</b>"+document.links[0].port+" "); document.write("<b>protocol:</b>"+document.links[0].protocol+" "); document.write("<b>target:</b>"+document.links[0].target+" "); document.write(" "); document.write("第二个连接的信息: "); document.write("<b>href:</b>"+document.links[1].href+" "); document.write("<b>pathname:</b>"+document.links[1].pathname+" "); document.write("<b>port:</b>"+document.links[1].port+" "); document.write("<b>protocol:</b>"+document.links[1].protocol+" "); document.write("<b>target:</b>"+document.links[1].target+" "); </script> </form> </div> </body> </html>
实例十(5)、
本实例主要介绍了图像属性的使用,模拟百度图片显示
<html> <head> <meta http-equiv="content-type" content="text/html; charset=gb2312"> <title>图像编程</title> <script language="javascript"> var imagearray = new array("http://yi-bo.com/article/uploadfiles/200610/2006101751022184.jpg","http://www.66ylw.com/article/uploadfiles/200610/2006101751024517.jpg","http://www.ishare.cc/d/247965-3/6519c067daa1f4b0f5eb44bc0ffd5da4.jpg"," http://www.dipan8.com/article/uploadfiles/200610/2006101751015599.jpg","http://www.66ylw.com/article/uploadfiles/200610/200610175936198.jpg","http://wanmeiad.net/article/uploadfiles/200610/2006101751024347.jpg"); var index = 0; function getnext() { index++; if(index < imagearray.length) //判断图像的下标是否小于数组的长度 { document.images["saint"].src=imagearray[index]; //如果小于,那么就显示该下标所指定的图像 } else { index = 0; document.images["saint"].src=imagearray[index]; //如果不小于,那么就显示第一幅图像,并把index值置为0 } } function getprev() { index--; if(index >= 0) //判断图像的下标是否大于0 { document.images["saint"].src=imagearray[index]; //如果大于,那么就显示该下标所指定的图像 } else { index = imagearray.length-1; document.images["saint"].src=imagearray[index]; //如果不大于,那么就显示最后一幅图像,并把index值置为数组长度减1 } } </script> </head> <body> <img name="saint" src="http://yi-bo.com/article/uploadfiles/200610/2006101751022184.jpg" width="400" height="300"> <a href="javascript:getnext()">下一幅</a> <a href="javascript:getprev()">上一幅</a> </body> </html>
实例十一(6)、
本实例主要介绍了鼠标动作的使用,这个例子太简单了,可以多找点别点资料看看!
<html> <head> <meta http-equiv="content-type" content="text/html; charset=gb2312"> <title>改变文字样式</title> <style type="text/css"> .red{ color:red; font-style:italic; font-size:32; } .blue{color:blue; font-size:36; } .black{color:black; } </style> <script language="javascript"> function color(e) { switch(e.srcelement.id){ //获得标记的id case "first": document.getelementbyid("first").classname="red"; //修改文字的样式 break; case "second": document.getelementbyid("second").classname="blue"; break; } } function cleartext(e) { switch(e.srcelement.id){ case "first": document.getelementbyid("first").classname="black"; break; case "second": document.getelementbyid("second").classname="black"; break; } } </script> </head> <body> <div id="first" onmouseover="color(event);" onmouseout="cleartext(event);">我是新手,摸一下</div> <div id="second" onmouseover="color(event);" onmouseout="cleartext(event);">老油条了</div> </body> </html>
实例十二、
本实例主要介绍了js访问xml节点的应用,读节点最基本的有2中方法,我分2个实例发上来,方便学习。
<html> <head> <title>访问xml文档</title> <script language="javascript"> function getinfo() { var document_xml = new activexobject("microsoft.xmldom"); document_xml.load("info.xml"); //加载info.xml var rootnode = document_xml.documentelement; //获得info.xml文档的根节点 var firstnode = rootnode.firstchild; //获得根标记的第一个子节点 var secondnode = rootnode.lastchild; //获得根标记的最后一个子节点 var namenode = firstnode.firstchild; var agenode = namenode.nextsibling; //获得namenode节点的下一个兄弟结点 var sexnode = firstnode.lastchild; var str = "名称是:"+namenode.firstchild.nodevalue+ "\n年龄是:"+agenode.firstchild.nodevalue+ "\n性别是:"+sexnode.firstchild.nodevalue+ "\n描述是:"+secondnode.firstchild.nodevalue; alert(str); } </script> </head> <body> <input type="button" name="submit" value="按钮" onclick="getinfo()"> </body> </html>
因为不能发附件,所有2个文件分别帖上来了,测试的时候分别保存即可。下面的是xml文件。说了一句废话,相信不会有人认为这个也是html!:)
<?xml version="1.0" encoding="gb2312"?> <info> <basic country="china"> <name num="3">霍元甲</name> <age>42</age> <sex>男</sex> </basic> <description>精武门的创始人</description> </info>
实例十三、
本实例主要介绍了js访问xml节点的应用,读节点的又一种方法,我上面发了一个,xml跟上面的一样就不重复了。
<html> <head> <title>按名称访问xml文档中的元素</title> <script language="javascript"> function getinfo() { var document_xml = new activexobject("microsoft.xmldom"); document_xml.load("info.xml"); var namenode = document_xml.getelementsbytagname("name"); //获得文档中所有<name>标记 var agenode = document_xml.getelementsbytagname("age"); //获得文档中所有<age>标记 var sexnode = document_xml.getelementsbytagname("sex"); //获得文档中所有<sex>标记 var desnode = document_xml.getelementsbytagname("description");//获得文档中所有<description标记> var str = "名称是:"+namenode(0).firstchild.nodevalue+ "\n年龄是:"+agenode(0).firstchild.nodevalue+ "\n性别是:"+sexnode(0).firstchild.nodevalue+ "\n描述是:"+desnode(0).firstchild.nodevalue; //将这些标记的文本内容添加进变量str中 alert(str); } </script> </head> <body> 点下我看看》》》<input type="button" name="submit" value="按钮" onclick="getinfo()"> </body> </html>
其它类似信息

推荐信息