-2
<html>
<head>
<script type="text/javascript">
function displayDate()

{
alert("hi");
 var d=new Date();
 var theDay=d.getDay();
 switch (theDay)
 {
 case 5:
   document.write("Finally Friday");
   break;
 case 6:
   document.write("Super Saturday");
   break;
 case 0:
   document.write("Sleepy Sunday");
   break;
 default:
   document.write("I'm looking forward to this weekend!");
 }

</script>
</head>
<body>
<h1>My First Web Page</h1>
<p id="demo">This is a paragraph</p>
alert("before function call");
<button type="button" onclick="displayDate()">Display Date</button>
alert("after function call");
</body>
</html>
4

3 回答 3

3

函数最后没有}

function displayDate()
{
alert("hi");
 var d=new Date();
 var theDay=d.getDay();
 switch (theDay)
 {
 case 5:
   document.write("Finally Friday");
   break;
 case 6:
   document.write("Super Saturday");
   break;
 case 0:
   document.write("Sleepy Sunday");
   break;
 default:
   document.write("I'm looking forward to this weekend!");
 }
}
于 2012-04-24T16:35:41.127 回答
2

您只是在函数末尾缺少一个 } 。

当某些事情没有按预期工作时,检查语法是您要做的第一件事。此外,在测试时检查浏览器中是否存在 javascript 错误:)

于 2012-04-24T16:46:19.000 回答
0

我刚刚测试了您的代码,它正在进行一些修改:

<html>
<head>
<script type="text/javascript">
function displayDate()

{
alert("hi");
 var d=new Date();
 var theDay=d.getDay();
 switch (theDay)
 {
 case 5:
   document.write("Finally Friday");
   break;
 case 6:
   document.write("Super Saturday");
   break;
 case 0:
   document.write("Sleepy Sunday");
   break;
 default:
   document.write("I'm looking forward to this weekend!");
 }
}
</script>
</head>
<body>
<h1>My First Web Page</h1>
<p id="demo">This is a paragraph</p>

<button type="button" onclick="displayDate()">Display Date</button>

</body>
</html>
于 2012-04-24T16:41:34.633 回答