Почему конкретная функция не работает в файле javascript, а хорошо работает в HTML-документе ?
I have two arrays of questions and answers. When the user types the right answer in the input field its background color changes. Everything is working fine if the script is in the html document but when it is in the javascript file, onclick function does not work. When the show answers button is clicked, the result div does not show, which is set to display: none. Could you please help me with finding the error ?
Что я уже пробовал:
<body> <body onload="resetValues()"> <div id="container"> <h1> Welcome to English Pupils </h1> <form> <label> 1. <input type="text" class="input" /> </label> <input type="text" class="output" /> <label> 2. <input type="text" class="input" /> </label> <input type="text" class="output" /> <label> 3. <input type="text" class="input" /> </label> <input type="text" class="output" /> <label> 4. <input type="text" class="input" /> </label> <input type="text" class="output" /> <label> 5. <input type="text" class="input" /> </label> <input type="text" class="output" /> </form> <a id="show" href="#result" onclick="printAnswers()"> Show Answers </a> <div id="result"> <a id="hide" href="#container"> Hide Answers </a> </div> </div> </body> ``` <style> #result { display: none; } ``` </style> <script> var questions = [ { question: "I like tea.", answer: "I do not like tea.", ans: "I don't like tea." }, { question: "You are my friend.", answer: "You are not my friend.", ans: "You aren't my friend." }, { question: "She is very naughty.", answer: "She is not very naughty.", ans: "She isn't very naughty." }, { question: "You can do it.", answer: "You cannot do it.", ans: "You can't do it." }, { question: "They are good.", answer: "They are not good.", ans: "They aren't good." }, ]; ``` // Answers printed in result div // var answers = [ { answer: "1. I do not like tea. <br> I don't like tea." }, { answer: "2. You are not my friend. <br> You aren't my friend." }, { answer: "3. She is not very naughty. <br> She isn't very naughty." }, { answer: "4. You cannot do it. <br> You can't do it." }, { answer: "5. They are not good. <br> They aren't good." }, ]; ``` // Showing questions in the input fields // $('document').ready(function() { $.each(questions, function(i, x) { $('.input').eq(i).val(x.question); $('.output').eq(i).attr("answer", x.answer); $('.output').eq(i).attr("ans", x.ans); $('.input').prop('readonly', true); }); $('.output').on("keyup", function() { checkAnswer(this); }); }); // Printing Answers // function printAnswers() { for (i = 0; i < answers.length; i++) { $("#result").append(answers[i].answer + "<br>"); } answers = []; } ``` // Changing background color if the answer is right // function checkAnswer(e) { let useranswer = $(e).val(); let realanswer = $(e).attr("answer"); let real = $(e).attr("ans"); if (realanswer == useranswer) { $(e).css("background-color", "#6ed66e"); } if (real == useranswer) { $(e).css("background-color", "#6ed66e"); } if ($(e).val() == "") { $(e).css("background-color", "white"); } } ``` // Empty answers fields on refresh // function resetValues() { var x = document.getElementsByClassName("output"); for (i = 0; i <= x.length; i++) { x.item(i).value = ""; } } ``` // Showing Answers // $("#show").click(function() { $("#result").css('display', 'flex'); $("#show").hide(); }); ``` // Hiding Answers // $("#hide").click(function() { $("#result").hide(); $("#show").show(); }); </script>>
MadMyche
Первое что нужно сделать это использовать инструменты разработчика Вашего браузера сначала на вкладке Сеть чтобы убедиться что файл JS загружен а затем на вкладке консоль чтобы проверить наличие ошибок JS
Bryian Tan
Где вы размещаете файл JavaScript? Покажите разметку.