首页 > 代码库 > javascript:入门笔记
javascript:入门笔记
1:html注释:
<html><body><script type="text/javascript"><!--document.write("Hello World!");//两个正斜杠是 JavaScript 的注释符号,它会阻止 JavaScript 编译器对这一行的编译--></script></body></html>
2:支持三元运算符
greeting=(visitor=="PRES")?"Dear President ":"Dear ";
3:三种消息框
警告框:alert("text")
确认框:confirm("text")
提示框:prompt("noon",12)
4:类似python的for in
var xvar mycars = new Array()mycars[0] = "Saab"mycars[1] = "Volvo"mycars[2] = "BMW"for (x in mycars){document.write(mycars[x] + "<br />")}
5:异常处理
<html><body><script type="text/javascript">while(true){var x=prompt("Enter a number between 0 and 10:","")try{ if(x>10) throw "Err1"else if(x<0)throw "Err2"elsebreak} catch(er){if(er=="Err1") alert("Error! The value is too high")if(er == "Err2") alert("Error! The value is too low") }}</script></body></html
6:显示时间
<html><head><script type="text/javascript">function startTime(){var today=new Date()var h=today.getHours()var m=today.getMinutes()var s=today.getSeconds()// add a zero in front of numbers<10m=checkTime(m)s=checkTime(s)document.getElementById(‘txt‘).innerHTML=h+":"+m+":"+st=setTimeout(‘startTime()‘,500)}function checkTime(i){if (i<10) {i="0" + i} return i}</script></head><body onl oad="startTime()"><div id="txt"></div></body></html>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。