簡體   English   中英

執行JavaScript

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM