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 x
var 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"
else
break
} 
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<10
m=checkTime(m)
s=checkTime(s)
document.getElementById(‘txt‘).innerHTML=h+":"+m+":"+s
t=setTimeout(‘startTime()‘,500)
}

function checkTime(i)
{
if (i<10) 
  {i="0" + i}
  return i
}
</script>
</head>

<body onload="startTime()">
<div id="txt"></div>
</body>

</html>

 

 

javascript:入门笔记,布布扣,bubuko.com

javascript:入门笔记

上一篇:JavaScript判断数据类型总结


下一篇:用javascript实现的单页应用骨架模型