Javascript函数调用无效

时间:2012-04-24 16:33:15

标签: javascript

<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>

3 个答案:

答案 0 :(得分: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!");
 }
}

答案 1 :(得分:2)

你在功能结束时错过了一个}。

当某些内容无法正常工作时,您首先要检查语法。另外,在测试时检查浏览器中是否存在javascript错误:)

答案 2 :(得分: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>