JavaScrip——简单练习(抓错误信息,for循环,日期)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<!--抓错误信息 -->
<div>
<input type="button" value="查看消息" onclick="message()" />
<script>
function message()
{
try{
adddlert("Welcome guest!");
}
catch(err)
{
txt="本页有一个错误。\n\n";
txt+="错误描述:" + err.message + "\n\n";
txt+="点击确定继续。\n\n";
alert(txt);
}
}
</script>
</div>
<div>
<input type="button" onclick="myFunction()" id="demo" value="遍历数组" />
<br />
<p id="wenben"></p>
<script>
function myFunction()
{
var x="";
for (var i=0;i<5;i++)
{
x=x + "The number is " + i+"<br />";
}
document.getElementById("wenben").innerHTML=x;
}
</script></div>
</body>
</html>
<script>
var time = new Date;
document.write(time)//取当前时间
document.write("<br />")
var day=new Date().getDay();//取当前时间的星期几(1,2,3.)
<!--容易理解的写法:var day=new Date()-->
<!-- var day1=day.getDay()-->
document.write("<br />")
document.write(day)
switch (day)
{
case 0:
x="Today it's Sunday";
break;
case 1:
x="Today it's Monday";
break;
case 2:
x="Today it's Tuesday";
break;
case 3:
x="Today it's Wednesday";
break;
case 4:
x="Today it's Thursday";
break;
case 5:
x="Today it's Friday";
break;
case 6:
x="Today it's Saturday";
break;
}
document.write(x)
</script>