码迷,mamicode.com
首页 > 编程语言 > 详细

javascript:入门笔记

时间:2014-07-16 21:41:54      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:style   blog   java   color   art   for   

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:入门笔记

标签:style   blog   java   color   art   for   

原文地址:http://www.cnblogs.com/qwj-sysu/p/3831176.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!