在我重新加载页面之前,JavaScript 不会从输入字段中获取国家/地区

我正在尝试使用 disease.sh API 制作一个冠状病毒病例跟踪器,但 JavaScript 无法从输入字段中获取国家/地区,除非刷新页面。我尝试删除事件侦听器并添加一个 onclick 属性,但那没有不工作,所以我猜这与未正确制作端点有关。


HTML:


<!DOCTYPE html>

<html>


<head>

    <title>Title</title>

    <!-- Required meta tags -->

    <meta charset="utf-8">

    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <link rel="stylesheet" href="style.css" type="text/css">

    <!-- Bootstrap CSS -->

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"

        integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

</head>


<body>

    <nav class="navbar navbar-expand-md bg-dark navbar-dark">

        <!-- Brand -->

        <a class="navbar-brand" href="#">

            <img src="./images/logo.png" class="img-fluid" alt="logo">

        </a>


        <p class="display-none">Corona Virus Case Tracker</p>


        <!-- Toggler/collapsibe Button -->

        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">

            <span class="navbar-toggler-icon"></span>

        </button>


        <!-- Navbar links -->

        <div class="collapse navbar-collapse" id="collapsibleNavbar">

            <ul class="navbar-nav ml-auto">

                <li class="nav-item" onmouseover="changeThing()" onmouseout="changeThingBack()">

                    <a class="nav-link active" id="list-item1" href="#">Home</a>

                </li>

                <li class="nav-item">

                    <a class="nav-link" href="#">Contact</a>

                </li>

                <li class="nav-item">

                    <a class="nav-link" href="#">About</a>

                </li>

                <li class="nav-item">

                    <a class="nav-link" href="#">More information</a>

                </li>

            </ul>

        </div>

    </nav>

胡子哥哥
浏览 109回答 2
2回答

慕标5832272

试试这个方法let case_results = document.querySelector(".case-results");let inputField = document.getElementById('submit-country');const submit = document.getElementById("submit-button");/*Test to change text on hover, to be replaced with iconsfunction changeThing() {&nbsp; document.getElementById('list-item1').innerHTML = 'hduashdas';}function changeThingBack() {&nbsp; document.getElementById('list-item1').innerHTML = 'Home';}*///const requestURL = `https://disease.sh/v3/covid-19/countries/Moldova?strict=true`;//Makes a call to the API and retrieves information about a specific countryconst getCases = async () => {let countrySubmitted = inputField.value;const URL = 'https://disease.sh/v3/covid-19/countries/';const lastParams = '?strict=true';const endpoint = `${URL}${countrySubmitted}${lastParams}`;&nbsp; try {&nbsp; &nbsp; const response = await fetch(endpoint, { cache: "no-cache" });&nbsp; &nbsp; if (response.ok) {&nbsp; &nbsp; &nbsp; const jsonResponse = await response.json();&nbsp; &nbsp; &nbsp; renderResponse(jsonResponse);&nbsp; &nbsp; }&nbsp; } catch (error) {&nbsp; &nbsp; console.log(error);&nbsp; }};//Clears the previous results in the div and displays new onesfunction displayCases(event) {&nbsp; while(case_results.firstChild) {&nbsp; &nbsp; case_results.removeChild(case_results.firstChild)&nbsp; }&nbsp; getCases();}submit.addEventListener('click', displayCases);const renderResponse = (res) => {if (res != null) {&nbsp; &nbsp; case_results.innerHTML = `cases in ${res.country} : ${res.cases}` + '<br>';&nbsp; }&nbsp;};.navigation-bar-logo {&nbsp; &nbsp; width: 240px;}img {&nbsp; width: 140px;}li {&nbsp; &nbsp; margin: 20px;&nbsp; &nbsp; font-size: 15px;}.case-results {&nbsp; color: blue;}a {&nbsp; color: white;&nbsp; text-decoration: none;&nbsp; text-transform: uppercase;&nbsp; font-family: Arial;&nbsp; display: inline-block;&nbsp; padding: 16px 32px;&nbsp; background-image: linear-gradient(120deg, transparent 50%, white 50%);&nbsp; background-size: 250%;&nbsp; transition: all 0.4s;&nbsp; letter-spacing: 2px;}p.display-none {&nbsp; font-size: 16px;&nbsp; color: white;&nbsp; margin: 0;&nbsp; padding: 0;}li:hover a {&nbsp; background-position: 100%;&nbsp; color: #313131 !important;&nbsp; transform: translateX(16px);}@media only screen and (max-width: 600px) {&nbsp; &nbsp; p.display-none {&nbsp; &nbsp; &nbsp; display: none;&nbsp; &nbsp; }&nbsp; &nbsp; li[class="nav-item"] {&nbsp; &nbsp; &nbsp; text-align: center;&nbsp; &nbsp; }&nbsp; }<!DOCTYPE html><html><head>&nbsp; &nbsp; <title>Title</title>&nbsp; &nbsp; <!-- Required meta tags -->&nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">&nbsp; &nbsp; <link rel="stylesheet" href="style.css" type="text/css">&nbsp; &nbsp; <!-- Bootstrap CSS -->&nbsp; &nbsp; <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"&nbsp; &nbsp; &nbsp; &nbsp; integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"></head><body>&nbsp; &nbsp; <nav class="navbar navbar-expand-md bg-dark navbar-dark">&nbsp; &nbsp; &nbsp; &nbsp; <!-- Brand -->&nbsp; &nbsp; &nbsp; &nbsp; <a class="navbar-brand" href="#">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="./images/logo.png" class="img-fluid" alt="logo">&nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; &nbsp; &nbsp; <p class="display-none">Corona Virus Case Tracker</p>&nbsp; &nbsp; &nbsp; &nbsp; <!-- Toggler/collapsibe Button -->&nbsp; &nbsp; &nbsp; &nbsp; <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="navbar-toggler-icon"></span>&nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; &nbsp; <!-- Navbar links -->&nbsp; &nbsp; &nbsp; &nbsp; <div class="collapse navbar-collapse" id="collapsibleNavbar">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ul class="navbar-nav ml-auto">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="nav-item" onmouseover="changeThing()" onmouseout="changeThingBack()">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="nav-link active" id="list-item1" href="#">Home</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="nav-item">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="nav-link" href="#">Contact</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="nav-item">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="nav-link" href="#">About</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="nav-item">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="nav-link" href="#">More information</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </nav>&nbsp;&nbsp; &nbsp; <input type="text" id="submit-country">&nbsp; &nbsp; <input type="submit" id="submit-button" value="get cases" />&nbsp; &nbsp; <div class="case-results">&nbsp; &nbsp; </div>&nbsp; &nbsp; <!-- Optional JavaScript -->&nbsp; &nbsp; <!-- jQuery first, then Popper.js, then Bootstrap JS -->&nbsp; &nbsp; <script src="main.js"></script>&nbsp; &nbsp; <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"&nbsp; &nbsp; &nbsp; &nbsp; integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"&nbsp; &nbsp; &nbsp; &nbsp; crossorigin="anonymous"></script>&nbsp; &nbsp; <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"&nbsp; &nbsp; &nbsp; &nbsp; integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"&nbsp; &nbsp; &nbsp; &nbsp; crossorigin="anonymous"></script>&nbsp; &nbsp; <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"&nbsp; &nbsp; &nbsp; &nbsp; integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"&nbsp; &nbsp; &nbsp; &nbsp; crossorigin="anonymous"></script></body></html>

呼啦一阵风

我已经修改了您的 javascript 并确认它可以工作。let case_results = document.querySelector(".case-results");let inputField = document.getElementById('submit-country');let countrySubmitted = inputField.value;const submit = document.getElementById("submit-button");const URL = 'https://disease.sh/v3/covid-19/countries/';const lastParams = '?strict=true';var endpoint = `${URL}${countrySubmitted}${lastParams}`;/*Test to change text on hover, to be replaced with iconsfunction changeThing() {&nbsp; document.getElementById('list-item1').innerHTML = 'hduashdas';}function changeThingBack() {&nbsp; document.getElementById('list-item1').innerHTML = 'Home';}*///const requestURL = `https://disease.sh/v3/covid-19/countries/Moldova?strict=true`;//Makes a call to the API and retrieves information about a specific countryconst getCases = async () => {&nbsp; try {&nbsp; &nbsp; const response = await fetch(endpoint, { cache: "no-cache" });&nbsp; &nbsp; if (response.ok) {&nbsp; &nbsp; &nbsp; const jsonResponse = await response.json();&nbsp; &nbsp; &nbsp; renderResponse(jsonResponse);&nbsp; &nbsp; }&nbsp; } catch (error) {&nbsp; &nbsp; console.log(error);&nbsp; }};//Clears the previous results in the div and displays new onesfunction displayCases(event) {&nbsp; countrySubmitted = inputField.value;&nbsp; endpoint = `${URL}${countrySubmitted}${lastParams}`;&nbsp; while(case_results.firstChild) {&nbsp; &nbsp; case_results.removeChild(case_results.firstChild)&nbsp; }&nbsp; getCases();}submit.addEventListener('click', displayCases);const renderResponse = (res) => {&nbsp; if (res != null) {&nbsp; &nbsp; console.log(res.updated);&nbsp; &nbsp; case_results.innerHTML = `cases in ${countrySubmitted} : ${res.cases}` + '<br>';&nbsp; }&nbsp;};
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript