如何使用javascript中的request从网站/api获取图像并在html中使用img src

代码的目标是从数组中获取用户输入的国家/地区,即 Item[4],将国家/地区附加到 url,然后使用请求从该 url 获取图像。我假设我需要使用 img src 在我的网站上显示图像。我做错了什么吗?我什么也没看到。另外,我正在尝试使用 javascript 来完成此任务。谢谢!API: https: //www.countryflags.io

personInfo.html

  <!DOCTYPE html>

        <html>


        <head>

            <meta charset="UTF-8">

            <title> Person Information Page </title>

            <style>

                h2{

                text-align: center;

                }

                label {

                    display: inline-block;

                    width: 150px;

                    text-align: left;

                }

            </style>

        </head>


        <body>

        <p style="text-decoration: underline;"><a href=http://127.0.0.1:5000/> Back to home </a></p>

        <h2 id="displayName" ></h2>

        <div class="block">

            <label>Born on: </label>

            <p id="birthdate"></p>

        </div>

        <div class="block">

            <label>Born in the country: </label>

            <p id="country"></p>

        </div>

        <div class="block">

            <label>Some facts about country: </label>

            <img id="countryFlag" src = country_flag; />

        </div>

        </body>

        </html>


慕婉清6462132
浏览 89回答 1
1回答

慕桂英3389331

试试这个:personInfo.html<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <title> Person Information Page </title>&nbsp; &nbsp; <style>&nbsp; &nbsp; &nbsp; &nbsp; h2{&nbsp; &nbsp; &nbsp; &nbsp; text-align: center;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; label {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display: inline-block;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width: 150px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text-align: left;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </style></head><body><p style="text-decoration: underline;"><a href=http://127.0.0.1:5000/> Back to home </a></p><h2 id="displayName" ></h2><div class="block">&nbsp; &nbsp; <label>Born on: </label>&nbsp; &nbsp; <p id="birthdate"></p></div><div class="block">&nbsp; &nbsp; <label>Born in the country: </label>&nbsp; &nbsp; <p id="country"></p></div><div class="block">&nbsp; &nbsp; <label>Some facts about country: </label>&nbsp; &nbsp; <img id="countryFlag" src = country_flag; /></div><script>var personInfoList = JSON.parse(sessionStorage.getItem("newList1"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(let item of personInfoList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;document.getElementById("displayName").innerHTML = item[0] + " " + item[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;document.getElementById("birthdate").innerHTML = item[2];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;document.getElementById("country").innerHTML = item[3];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;country_flag = `https://www.countryflags.io/${item[3]}/shiny/64.png`;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;document.getElementById("countryFlag").src = country_flag;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</script></body></html>添加名称.html<!DOCTYPE html><html><head><meta charset="UTF-8"><title> Add Name Page </title><style>&nbsp; &nbsp; body {&nbsp; &nbsp; &nbsp; &nbsp; text-align: center;&nbsp; &nbsp; }&nbsp; &nbsp; form {&nbsp; &nbsp; &nbsp; &nbsp; position:relative;&nbsp; &nbsp; &nbsp; &nbsp; top:20px;&nbsp; &nbsp; &nbsp; &nbsp; display: inline-block;&nbsp; &nbsp; }&nbsp; &nbsp; label {&nbsp; &nbsp; &nbsp; &nbsp; display: inline-block;&nbsp; &nbsp; &nbsp; &nbsp; width: 150px;&nbsp; &nbsp; &nbsp; &nbsp; text-align: left;&nbsp; &nbsp; }&nbsp; &nbsp; p {&nbsp; &nbsp; &nbsp; &nbsp; position: relative;&nbsp; &nbsp; &nbsp; &nbsp; top: -43px;&nbsp; &nbsp; &nbsp; &nbsp; left: 410px;&nbsp; &nbsp; }</style>{% extends "navigation.html" %}{% block content %}&nbsp;</head><body>&nbsp; &nbsp; &nbsp;<form action="home.html" >&nbsp; &nbsp; &nbsp;<div class="block">&nbsp; &nbsp; &nbsp; &nbsp; <label>First name: </label>&nbsp; &nbsp; &nbsp; &nbsp; <input type='text' input name='firstname' id='firstname'>&nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp;<div class="block">&nbsp; &nbsp; &nbsp; &nbsp; <label>Last name: </label>&nbsp; &nbsp; &nbsp; &nbsp; <input type='text' input name='lastname' id='lastname'>&nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp;<div class="block">&nbsp; &nbsp; &nbsp; &nbsp; <label>Birthday: </label>&nbsp; &nbsp; &nbsp; &nbsp; <input type='text' input name='birthday' id='birthday' placeholder="mm/dd/yyyy">&nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; <div class="block">&nbsp; &nbsp; &nbsp; &nbsp; <label>Country of Origin: </label>&nbsp; &nbsp; &nbsp; &nbsp; <input type='text' input name='countryOfOrigin' id='countryOfOrigin'>&nbsp; &nbsp; </div>&nbsp; &nbsp; <p>&nbsp; &nbsp; <input type="button" id="add" value="Submit" onclick= "passVal(); window.location.href = '/';">&nbsp; &nbsp; </p>&nbsp; &nbsp; </form>&nbsp; &nbsp; <script>&nbsp; &nbsp; function passVal() {&nbsp; &nbsp; var prevValue = localStorage.getItem("newList1");&nbsp; &nbsp; var newList = []&nbsp; &nbsp; if(prevValue) {&nbsp; &nbsp; &nbsp; &nbsp; newList = JSON.parse(prevValue);&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; var newFirstName = document.getElementById("firstname").value;&nbsp; &nbsp; var newLastName = document.getElementById("lastname").value;&nbsp; &nbsp; var newBirthday = document.getElementById("birthday").value;&nbsp; &nbsp; var newCOO = document.getElementById("countryOfOrigin").value;&nbsp; &nbsp; newList.push([newFirstName, newLastName, newBirthday, newCOO]);&nbsp; &nbsp; sessionStorage.setItem("newList1", JSON.stringify(newList)); }&nbsp; &nbsp; </script></body>{% endblock %}在 add_name.html 中,我更改了“var newList;” 到“var newList = []”。在 personInfo.html 中,不需要获取图像,只需将图像路径分配给 src 即可。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5