简体   繁体   English

执行JavaScript

[英]execution of JavaScript

could you help me understand what's wrong with the code and why the button doesn't work when I click on it. 您能否帮助我了解代码的问题以及为什么单击按钮后按钮不起作用? I believe that I missed some little part and I can't find it. 我相信我错过了一小部分,但找不到。

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

I would really appreciate the help. 我非常感谢您的帮助。 I really lack any ideas of why the javascript doesn't work here. 我真的不知道为什么javascript在这里不起作用。

You are missing () in AgeCh. 您在AgeCh中丢失() Function should be defined like AgeCh() 函数应定义为AgeCh()

Corrected code: 更正的代码:

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

Seems to be an error with defining your function. 定义函数似乎是一个错误。 I redefined your function using the let AgeCh = function() {} and this seemed to get your function to work. 我使用let AgeCh = function() {}重新定义了您的函数,这似乎可以使您的函数正常工作。

https://plnkr.co/edit/rHBbofiwvO55bCLDA4jg?p=preview https://plnkr.co/edit/rHBbofiwvO55bCLDA4jg?p=preview

docs on functions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions 函数文档: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Functions

yes check your function declaration is wrong Its like 是的,请检查您的函数声明是否错误

 function AgeCh 
  {

change to 改成

function AgeCh()
{

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM