[英]execution of JavaScript
您能否帮助我了解代码的问题以及为什么单击按钮后按钮不起作用? 我相信我错过了一小部分,但找不到。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Conditions</title>
<link href="css/styles.css" rel="stylesheet">
<script>
function AgeCh
{
var Age = prompt("Please enter your age here","from 4 to 100");
if (Age >= 16)
{
document.write("You are old enough to drive.");
}
else if (Age >=21)
{
document.write("You are old enough to drive and vote.");
}
else if (Age >=65)
{
document.write("You are old enough to drive, vote and retire.");
}
else
{
document.write ("thinks");
}
}
</script>
</head>
<body>
<div class="classselector">
<button type = "button" onclick="AgeCh()">Age Checker</button>
</div>
</body>
</html>
我非常感谢您的帮助。 我真的不知道为什么javascript在这里不起作用。
您在AgeCh中丢失()
。 函数应定义为AgeCh()
更正的代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Conditions</title>
<link href="css/styles.css" rel="stylesheet">
<script>
function AgeCh()
{
var Age = prompt("Please enter your age here","from 4 to 100");
if (Age >= 16)
{
document.write("You are old enough to drive.");
}
else if (Age >=21)
{
document.write("You are old enough to drive and vote.");
}
else if (Age >=65)
{
document.write("You are old enough to drive, vote and retire.");
}
else
{
document.write ("thinks");
}
}
</script>
</head>
<body>
<div class="classselector">
<button type = "button" onclick="AgeCh()">Age Checker</button>
</div>
</body>
</html>
定义函数似乎是一个错误。 我使用let AgeCh = function() {}
重新定义了您的函数,这似乎可以使您的函数正常工作。
https://plnkr.co/edit/rHBbofiwvO55bCLDA4jg?p=preview
函数文档: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Functions
是的,请检查您的函数声明是否错误
function AgeCh
{
改成
function AgeCh()
{
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.