英文:
How to bring and image request from JS to display in HTML
问题
我正在尝试使用PokeAPI创建一个宝可梦队伍生成器,我可以成功获取文本信息,但无法找到显示图像的方法。
const name = document.getElementById("name");
const pokemonImage = document.getElementById('monster');
const height = document.getElementById("height");
const weight = document.getElementById("weight");
const button = document.querySelector(".button");
button.addEventListener('click', (e) => {
e.preventDefault();
const randomNumber = Math.ceil(Math.random() * 905);
fetch(`https://pokeapi.co/api/v2/pokemon/${randomNumber}/`)
.then(response => response.json())
.then(pokemon => {
console.log(pokemon);
name.innerHTML = pokemon['name'];
height.innerHTML = pokemon['height'];
weight.innerHTML = pokemon['weight'];
id.innerHTML = `宝可梦编号: ${pokemon['id']}`;
pokemonImage.src = `https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokemon['id']}.png`;
});
});
我尝试使用ID,但似乎在互联网上找不到一个好的答案。
英文:
I am trying to make a pokemon team generator with pokeapi and i am able to fetch the text info fine but i cant figure out a way to display the image.
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Java Script 201</title>
</head>
<body>
<img src="" id="monster">
<h3>Name:</h3>
<div id="name"></div>
<h3>Height:</h3>
<div id="height"></div>
<h3>Weight:</h3>
<div id="weight"></div>
<button class="button">Generate Pokemon Team!</button>
<script src="script.js"></script>
</body>
</html>
const name = document.getElementById("name");
const pokemonImage = document.getElementById('pokemon')
const height = document.getElementById("height");
const weight = document.getElementById("weight");
const button = document.querySelector(".button");
button.addEventListener('click', (e) => {
e.preventDefault()
const randomNumber = Math.ceil (Math.random() * 905)
fetch (`https://pokeapi.co/api/v2/pokemon/${randomNumber}/`)
.then (response => response.json())
.then (pokemon => {
console.log(pokemon)
name.innerHTML = pokemon ['name'];
height.innerHTML = pokemon ['height'];
weight.innerHTML = pokemon ['weight'];
id.innerHTML = (`Pokemon Id: ${Pokemon['id']}`);
pokemonImage.innerHTML =`<img id='monster' src=https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${Pokemon['id']}.png>`
})
})
I have tried using ID and just cant seem to find a good anwsxer on the internet.
答案1
得分: 1
选择页面上的<img>
元素并设置其src
属性。
const name = document.getElementById("name");
const pokemonImage = document.getElementById('pokemonImg')
const height = document.getElementById("height");
const weight = document.getElementById("weight");
const button = document.querySelector(".button");
button.addEventListener('click', (e) => {
e.preventDefault()
const randomNumber = Math.ceil(Math.random() * 905)
fetch(`https://pokeapi.co/api/v2/pokemon/${randomNumber}/`)
.then(response => response.json())
.then(pokemon => {
name.innerHTML = pokemon['name'];
height.innerHTML = pokemon['height'];
weight.innerHTML = pokemon['weight'];
pokemonImage.src = `https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokemon['id']}.png`;
})
})
<img id="pokemonImg">
<h3>Name:</h3>
<div id="name"></div>
<h3>Height:</h3>
<div id="height"></div>
<h3>Weight:</h3>
<div id="weight"></div>
<button class="button">Generate Pokemon Team!</button>
请注意,代码部分未进行翻译。
英文:
Select the <img>
element on your page and set its src
property.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const name = document.getElementById("name");
const pokemonImage = document.getElementById('pokemonImg')
const height = document.getElementById("height");
const weight = document.getElementById("weight");
const button = document.querySelector(".button");
button.addEventListener('click', (e) => {
e.preventDefault()
const randomNumber = Math.ceil(Math.random() * 905)
fetch(`https://pokeapi.co/api/v2/pokemon/${randomNumber}/`)
.then(response => response.json())
.then(pokemon => {
name.innerHTML = pokemon['name'];
height.innerHTML = pokemon['height'];
weight.innerHTML = pokemon['weight'];
pokemonImage.src = `https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokemon['id']}.png`;
})
})
<!-- language: lang-html -->
<img id="pokemonImg">
<h3>Name:</h3>
<div id="name"></div>
<h3>Height:</h3>
<div id="height"></div>
<h3>Weight:</h3>
<div id="weight"></div>
<button class="button">Generate Pokemon Team!</button>
<!-- end snippet -->
答案2
得分: 0
你只需获取带有id的标签并设置属性。
const nameElem = document.getElementById("name");
const pokemonImage = document.getElementById('pokemon')
const heightElem = document.getElementById("height");
const weightElem = document.getElementById("weight");
const button = document.querySelector(".button");
button.addEventListener('click', (e) => {
e.preventDefault()
const randomNumber = Math.ceil(Math.random() * 905)
fetch(`https://pokeapi.co/api/v2/pokemon/${randomNumber}/`)
.then(response => response.json())
.then(pokemon => {
const {
name,
height,
weight,
sprites
} = pokemon;
console.log(name)
nameElem.innerHTML = name;
heightElem.innerHTML = height;
weightElem.innerHTML = weight;
pokemonImage.setAttribute('src', sprites.back_default);
})
})
<img src="" id="pokemon">
<h3>Name:</h3>
<div id="name"></div>
<h3>Height:</h3>
<div id="height"></div>
<h3>Weight:</h3>
<div id="weight"></div>
<button class="button">Generate Pokemon Team!</button>
英文:
You can just get the tag wit the id and set the attribute
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const nameElem = document.getElementById("name");
const pokemonImage = document.getElementById('pokemon')
const heightElem = document.getElementById("height");
const weightElem = document.getElementById("weight");
const button = document.querySelector(".button");
button.addEventListener('click', (e) => {
e.preventDefault()
const randomNumber = Math.ceil(Math.random() * 905)
fetch(`https://pokeapi.co/api/v2/pokemon/${randomNumber}/`)
.then(response => response.json())
.then(pokemon => {
const {
name,
height,
weight,
sprites
} = pokemon;
console.log(name)
nameElem.innerHTML = name;
heightElem.innerHTML = height;
weightElem.innerHTML = weight;
pokemonImage.setAttribute('src', sprites.back_default);
})
})
<!-- language: lang-html -->
<img src="" id="pokemon">
<h3>Name:</h3>
<div id="name"></div>
<h3>Height:</h3>
<div id="height"></div>
<h3>Weight:</h3>
<div id="weight"></div>
<button class="button">Generate Pokemon Team!</button>
<!-- end snippet -->
答案3
得分: 0
以下是您要翻译的代码的中文翻译:
你只是犯了一些疏忽和拼写错误。否则,你的代码是正确的。
工作代码:
// 开始代码片段: js 隐藏: false 控制台: true 转译: false
// 语言: lang-js
const name = document.getElementById("name");
const pokemonImage = document.getElementById('pokemon')
const height = document.getElementById("height");
const weight = document.getElementById("weight");
const button = document.querySelector(".button");
button.addEventListener('click', (e) => {
e.preventDefault()
const randomNumber = Math.ceil(Math.random() * 905)
fetch(`https://pokeapi.co/api/v2/pokemon/${randomNumber}/`)
.then(response => response.json())
.then(pokemon => {
console.log(pokemon)
name.innerHTML = pokemon['name'];
height.innerHTML = pokemon['height'];
weight.innerHTML = pokemon['weight'];
pokemonImage.innerHTML = `<img id='monster' src=https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokemon['id']}.png>`
})
})
// 语言: lang-html
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Java Script 201</title>
</head>
<body>
<div id="pokemon"></div>
<h3>Name:</h3>
<div id="name"></div>
<h3>Height:</h3>
<div id="height"></div>
<h3>Weight:</h3>
<div id="weight"></div>
<button class="button">Generate Pokemon Team!</button>
</body>
</html>
// 结束代码片段
希望这对您有所帮助。如果您需要进一步的帮助,请随时告诉我。
英文:
You just have made some inattention and typo errors. Otherwise you code is correct.
The working code :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const name = document.getElementById("name");
const pokemonImage = document.getElementById('pokemon')
const height = document.getElementById("height");
const weight = document.getElementById("weight");
const button = document.querySelector(".button");
button.addEventListener('click', (e) => {
e.preventDefault()
const randomNumber = Math.ceil (Math.random() * 905)
fetch (`https://pokeapi.co/api/v2/pokemon/${randomNumber}/`)
.then (response => response.json())
.then (pokemon => {
console.log(pokemon)
name.innerHTML = pokemon ['name'];
height.innerHTML = pokemon ['height'];
weight.innerHTML = pokemon ['weight'];
pokemonImage.innerHTML =`<img id='monster' src=https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokemon['id']}.png>`
})
})
<!-- language: lang-html -->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Java Script 201</title>
</head>
<body>
<div id="pokemon"></div>
<h3>Name:</h3>
<div id="name"></div>
<h3>Height:</h3>
<div id="height"></div>
<h3>Weight:</h3>
<div id="weight"></div>
<button class="button">Generate Pokemon Team!</button>
</body>
</html>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论