已阅读5页,还剩47页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
菜鸟学习javascript实例教程用JS显示文字的例子:document.write(Hello World!)用HTML标签来格式化文本的例子:document.write(Hello World!)书写JS位置的例子:打开页面弹出窗口的例子function message()alert(网页教学网欢迎你的光临)在BODY区内输出显示文本的例子:document.write(欢迎光临网页教学网)调用其它的一个JS文件的例子:The actual script is in an external script file called xxx.js.变量的使用变量使用的例子:var name = Hegedocument.write(name)document.write( name )This example declares a variable, assigns a value to it, and then displays the variable.Then the variable is displayed one more time, only this time as a heading.函数的例子函数使用的一个例子:function myfunction()alert(HELLO)By pressing the button, a function will be called. The function will alert a message.带一个参数的函数的例子:function myfunction(txt)alert(txt)By pressing the button, a function with an argument will be called. The function will alertthis argument.不同的两个参数调用函数的例子: function myfunction(txt) alert(txt) When you click on one of the buttons, a function will be called. The function will alertthe argument that is passed to it. 利用函数返回值的例子:function myFunction()return (Hello, have a nice day!)document.write(myFunction()The script in the body section calls a function.The function returns a text.带参数的函数返回值的例子:function total(numberA,numberB)return numberA numberBdocument.write(total(2,3)The script in the body section calls a function with two arguments, 2 and 3.The function returns the sum of these two arguments.条件语句的例子简单条件语句的例子:var d = new Date()var time = d.getHours()if (time 10) document.write(Good morning)This example demonstrates the If statement.If the time on your browser is less than 10,you will get a Good morning greeting.if . else 的条件语句的例子:var d = new Date()var time = d.getHours()if (time 10) document.write(Good morning)elsedocument.write(Good day)This example demonstrates the If.Else statement.If the time on your browser is less than 10,you will get a Good morning greeting.Otherwise you will get a Good day greeting.用随机数产生连接的例子:var r=Math.random()if (r0.5) document.write(Learn Web Development!)elsedocument.write(Visit Refsnes Data!)多条件的语句实现的例子:var d = new Date()theDay=d.getDay()switch (theDay)case 5:document.write(Finally Friday)breakcase 6:document.write(Super Saturday)breakcase 0:document.write(Sleepy Sunday)breakdefault:document.write(Im really looking forward to this weekend!)This example demonstrates the switch statement.You will receive a different greeting based on what day it is.Note that Sunday=0, Monday=1, Tuesday=2, etc.循环的例子:for循环的一个例子:for (i = 0; i = 5; i )document.write(The number is i)document.write()Explanation:The for loop sets i equal to 0.As long as i is less than , or equal to, 5, the loop will continue to run.i will increase by 1 each time the loop runs.复杂循环的一个例子:for (i = 1; i = 6; i )document.write(This is header i)document.write()按条件循环while的例子:i = 0while (i = 5)document.write(The number is i)document.write()i Explanation:i equal to 0.While i is less than , or equal to, 5, the loop will continue to run.i will increase by 1 each time the loop runs.do.while循环的例子:i = 0dodocument.write(The number is i)document.write()i while (i = 5)Explanation:i equal to 0.The loop will runi will increase by 1 each time the loop runs.While i is less than , or equal to, 5, the loop will continue to run.字符串对象的例子:检测字符串长度的例子:var str=W3Schools is great!document.write( str )document.write(str.length)检测子字符串位置的例子:var str=W3Schools is great!var pos=str.indexOf(School)if (pos=0)document.write(School found at position: )document.write(pos )elsedocument.write(School not found!)This example tests if a string contains a specified word. If the word is found it returns the position of the first character of the word in the original string. Note: The first position in the string is 0! 检测子字符串是否存在的例子:var str = W3Schools is great!document.write(str.match(great)This example tests if a string contains a specified word. If the word is found it returns the word.取子字符串的例子:var str=W3Schools is great!document.write(str.substr(2,6)document.write()document.write(str.substring(2,6)The substr() method returns a specified part of a string. If you specify (2,6) the returned string will be from the second character (start at 0) and 6 long.The substring() method also returns a specified part of a string. If you specify (2,6) it returns all characters from the second character (start at 0) and up to, but not including, the sixth character.转换字符串的大小写var str=(Hello JavaScripters!)document.write(str.toLowerCase()document.write()document.write(str.toUpperCase()数组对象的实例数组简单应用的例子:var famname = new Array(6)famname0 = Jan Egilfamname1 = Tovefamname2 = Hegefamname3 = Stalefamname4 = Kai Jimfamname5 = Borgefor (i=0; i6; i )document.write(famnamei )另一种使用数组的方法:var famname = new Array(Jan Egil,Tove,Hege,Stale,Kai Jim,Borge)for (i=0; ifamname.length; i )document.write(famnamei )使用数组的一些属性和方法:var famname = new Array(3)famname0 = Janifamname1 = Tovefamname2 = Hegedocument.write(famname.length )document.write(famname.join(.) )document.write(famname.reverse() )document.write(famname.push(Ola,Jon) )document.write(famname.pop() )document.write(famname.shift() )数组的两个方法concat和slicevar famname = new Array(3)famname0 = Janifamname1 = Tovefamname2 = Hegevar famname2 = new Array(3)famname20 = Johnfamname21 = Andyfamname22 = Wendyvar famname3 = new Array(Stale,Borge)document.write(famname.join() )document.write(famname.concat(famname2) )document.write(famname.concat(famname2,famname3) )document.write(famname.slice(1) )日期相关例子:显示今天的日期:var d = new Date()document.write(d.getDate()document.write(.)document.write(d.getMonth() 1)document.write(.)document.write(d.getFullYear()显示当前的时间:var d = new Date()document.write(d.getHours()document.write(.)document.write(d.getMinutes()document.write(.)document.write(d.getSeconds()设置日期:var d = new Date()d.setFullYear(1990)document.write(d)UTC时间:var d = new Date()document.write(d.getUTCHours()document.write(.)document.write(d.getUTCMinutes()document.write(.)document.write(d.getUTCSeconds()显示当前的星期:var d=new Date()var weekday=new Array(Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday)document.write(Today is weekdayd.getDay() 显示当前的日期和星期:var d=new Date()var weekday=new Array(Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday)var monthname=new Array(Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)document.write(weekdayd.getDay() )document.write(d.getDate() . )document.write(monthnamed.getMonth() )document.write(d.getFullYear()一个走动的时间:var timer = nullfunction stop()clearTimeout(timer)function start()var time = new Date()var hours = time.getHours()var minutes = time.getMinutes()minutes=(minutes 10) ? 0 : ) minutesvar seconds = time.getSeconds()seconds=(seconds 10) ? 0 : ) secondsvar clock = hours : minutes : secondsdocument.forms0.display.value = clocktimer = setTimeout(start(),1000)数学对象的例子:document.write(Math.round(7.25)产生0-1之间的随机数的例子document.write(Math.random()产生0-10的随机数的例子no=Math.random()*10document.write(Math.round(no)求最大数的例子:document.write(Math.max(2,4)求最小数的例子:document.write(Math.min(2,4)Convert Celsius to Fahrenheitfunction convert(degree)if (degree=C)F=document.myform.celsius.value * 9 / 5 32document.myform.fahrenheit.value=Math.round(F)else C=(document.myform.fahrenheit.value -32) * 5 / 9document.myform.celsius.value=Math.round(C)Insert a number in either input field, and the number will be converted intoeither Celsius or Fahrenheit. degrees Celsiusequals degrees Fahrenheit Note that the Math.round method is used,so that the result will be returned as a whole number.转变字符为数字的例子function toUnicode()var str=document.myForm.myInput.valueif (str!=)unicode=str.charCodeAt(0)document.myForm.unicode.value=unicodeWrite a character:The characters Unicode:超级连接对象用按钮来改变连接位置的例子:function myHref()document.getElementById(myAnchor).innerText=Visit W3Schoolsdocument.getElementById(myAnchor).href=Visit Microsoft改变连接的打开方式的例子:function myTarget()document.getElementById(myAnchor).target=_blankVisit W3Schools Try the link before and after you have pressed the button! 使连接获得焦点和失去焦点a:active color:bluefunction getfocus()document.getElementById(w3s).focus()function losefocus()document.getElementById(w3s).blur()Visit W3S连接打开的方式function linkToAnchor(num)var win2=open(tryjs_anchor2.htm,secondLinkWindow,scrollbars=yes,width=250,height=200)win2.location.hash=numLinks and AnchorsClick on a button to display that anchor in window 2!按钮对象创建一个按钮function show_alert()alert(Hello World!)document.all(myButton).focus()显示按钮的名称The forms name is: 显示表单中各个项的名称function showFormElements(theForm)str=Form Elements: for (i=0; itheForm.length; i )str = n theFalert(str)First name: Last name: 副选框的选择和取消function check()var x=document.forms.myFormx0.checked=truefunction uncheck()var x=document.forms.myFormx0.checked=false表单中的副选框的选择与取消function check()coffee=document.forms0.coffeeanswer=document.forms0.answertxt=for (i=0;icoffee.length; i)if (coffeei.checked)txt=txt coffeei.value answer.value=You ordered a coffee with txtHow would you like your coffee?With creamWith su
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 抢救车使用试题及答案
- 甘肃省白银市平川区2025-2026学年三年级上学期期末数学试题(含答案)
- 辅警的教育培训课件
- 妊娠剧吐急诊护理的病例分析
- 糖尿病足部护理创新模式
- 2026年深圳中考语文考前终极预测试卷(附答案可下载)
- 《GAT 16.86-2012道路交通管理信息代码 第86部分:剧毒化学品公路运输通行证通行区域代码》专题研究报告
- 2026年深圳中考物理寒假提分特训试卷(附答案可下载)
- 2026年大学大二(口腔修复学)口腔修复临床技术测试题及答案
- 水电工施工技能培训课件
- 同等学力硕士学位协议书
- 维修工作计划模板范文
- DB13(J)-T 8401-2021 钢丝网片复合保温板应用技术标准
- 韩语四六级试题及答案
- 餐厅控烟制度管理制度
- 小学生沟通与礼仪课件
- 设计公司部门领导发言稿
- pid控制介绍课件
- 深圳科技馆新馆展教工程常设展区整体展教方案
- 《重庆市北碚区高标准农田建设规划2021-2030年》
- 湖南省娄底市娄星区2024-2025学年九年级上学期期末考试道德与法治试卷(含答案)
评论
0/150
提交评论