错误,== 不起作用,输入 == 字母不起作用

哈拉斯马斯里
<!DOCTYPE html>
<html lang="en">
<!-------------------------------------- Hot HTML Head ------------------------------------------->
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Table hiragana</title>
    <!------------------------------------- CSS sheesh ----------------------------------------->
        <style>
            * {
                margin: 0; color: rgb(40,40,40); text-decoration: none; font-size: 30px;
                font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
            }
                .menu {
                    display: flex; flex-direction: row; justify-content: center; margin-top: 1%;
                }
                .menu , a {
                    font-size: 12px; margin-left: 2%; margin-right: 2%;
                }
            #container {
                margin-top: 10%;
            }

        </style>
</head>

<!-------------------------------------- Hot HTML Bod ------------------------------------------->
<body>
    <center>
        <div class="menu">
            <a href="index.html">HOME</a>
            <a href="table.html">TABLE</a>
            <a href="random.html">RANDOM</a>
        </div>

        <div id="container">
            <p id="japanletter">letter</p>
            <input id="input"></input>
            <p onclick='run()' style="cursor:pointer;padding:10px;background-color:#eee;width:50px;margin:1%;font-size:12px;">check</p>
            <p id="status">status</p>
        </div>
    </center>

    <script>
        // randomize the letter
        function randomize() {
            var random = Math.floor(Math.random() * 2) + 1 , letter = "" , input = "";  // output will 1-46 because hiragana table has 46 chars

            // process randomizer for letter
            if (random == 1) {
                letter = "あ";
            }
            else {
                letter = "い";
            }

        // output random letters
        document.getElementById('japanletter').innerHTML = letter;

        }

        // onblur get value from input
        function run() {
            // get value in input
            var input = document.getElementById('input').value;
            compare();
        }

        // function compare letter and input
        function compare() {
                // if input equivalent letter
                if (input == letter) {
                    // output them and direct to return randomize
                    document.getElementById('status').innerHTML = "correct";
                    randomize();
                }
            }

        // call out function
        randomize();
    </script>
</body>
</html>
肖恩肖

我刚搬出两个变量inputletter该方法的randomize(),所以它可以在其他的方法来访问run()(获取的值input)和compare()(两个值进行比较inputletter)。

请尝试以下代码。

<!DOCTYPE html>
<html lang="en">
<!-------------------------------------- Hot HTML Head ------------------------------------------->
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Table hiragana</title>
    <!------------------------------------- CSS sheesh ----------------------------------------->
        <style>
            * {
                margin: 0; color: rgb(40,40,40); text-decoration: none; font-size: 30px;
                font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
            }
                .menu {
                    display: flex; flex-direction: row; justify-content: center; margin-top: 1%;
                }
                .menu , a {
                    font-size: 12px; margin-left: 2%; margin-right: 2%;
                }
            #container {
                margin-top: 10%;
            }

        </style>
</head>

<!-------------------------------------- Hot HTML Bod ------------------------------------------->
<body>
    <center>
        <div class="menu">
            <a href="index.html">HOME</a>
            <a href="table.html">TABLE</a>
            <a href="random.html">RANDOM</a>
        </div>

        <div id="container">
            <p id="japanletter">letter</p>
            <input id="input"></input>
            <p onclick='run()' style="cursor:pointer;padding:10px;background-color:#eee;width:50px;margin:1%;font-size:12px;">check</p>
            <p id="status">status</p>
        </div>
    </center>

    <script>
    // randomize the letter 1-46 because hiragana table has 46 chars
        var letter = "" , input = "";  // output will
        function randomize() {
            var random = Math.floor(Math.random() * 2) + 1;

            // process randomizer for letter
            if (random == 1) {
                letter = "あ";
            }
            else {
                letter = "い";
            }

        // output random letters
        document.getElementById('japanletter').innerHTML = letter;

        }

        // onblur get value from input
        function run() {
            // get value in input
            input = document.getElementById('input').value;
            compare();
        }

        // function compare letter and input
        function compare() {
                // if input equivalent letter
                if (input == letter) {
                    // output them and direct to return randomize
                    document.getElementById('status').innerHTML = "correct";
                    randomize();
                }
            }

        // call out function
        randomize();
    </script>
</body>
</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章