Javascript 函数调用不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10302111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript function call is not working
提问by abson
<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>
回答by hkutluay
function has not } at last
函数最后没有}
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!");
}
}
回答by vipergtsrz
You're just missing a } at the end of your function.
您只是在函数末尾缺少一个 } 。
Checking syntax is the first thing you want to do when something isn't working as intended. Also, checking to see if there's a javascript error in your browser when testing :)
当某些事情没有按预期工作时,检查语法是您要做的第一件事。另外,在测试时检查浏览器中是否存在 javascript 错误:)
回答by Satya
I just now tested your code and it is working with a few modifications :
我刚刚测试了您的代码,并进行了一些修改:
<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>