你能帮我了解代码有什么问题,以及为什么当我点击它时按钮不起作用。我相信我错过了一些小部分,我找不到它。
<!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/en-US/docs/Web/JavaScript/Guide/Functions
请检查您的函数声明是否错误它喜欢
function AgeCh
{
更改为
function AgeCh()
{