Javascript 我需要使用fetch为数组中的每个图像向DOM添加图像元素
添加JavaScript: 在页面加载时,使用上面的url获取图像, ,将响应解析为JSONJavascript 我需要使用fetch为数组中的每个图像向DOM添加图像元素,javascript,image,api,fetch,Javascript,Image,Api,Fetch,添加JavaScript: 在页面加载时,使用上面的url获取图像, ,将响应解析为JSON ,将数组中每个图像的图像元素添加到DOM中代码中几乎没有错误 它应该resp.json()而不是resp.json 然后需要在传入数据images.message.forEach()中的message属性上循环 设置img.src非img.innerHTML 函数ceo(){ 常量imgUrl=”https://dog.ceo/api/breeds/image/random/4" 获取(imgU
,将数组中每个图像的图像元素添加到DOM中代码中几乎没有错误
resp.json()
而不是resp.json
images.message.forEach()中的message
属性上循环
img.src
非img.innerHTML
函数ceo(){
常量imgUrl=”https://dog.ceo/api/breeds/image/random/4"
获取(imgUrl)
.then(resp=>resp.json())
.then(json=>renderImg(json))
}
//需要概念方面的帮助
函数渲染(图像){
const loc=document.querySelector('div')
images.message.forEach(Image=>{
const img=document.createElement('img');
img.src=图像;
附件儿童(img)位置
});
}
document.addEventListener('DOMContentLoaded',()=>{
首席执行官()
})
但问题是什么?另外resp=>resp.json()
非常感谢much@feli当一个答案解决了你的问题,你可以考虑投票和/或标记它被接受,以表明问题得到解决:
function ceo() {
const imgUrl = "https://dog.ceo/api/breeds/image/random/4"
fetch(imgUrl)
.then(resp => resp.json)
.then(json => renderImg(json))
}
// needed help with concepts
function renderImg(images) {
const loc = document.querySelector('div')
images.(Image => {
const img = document.createElement('img');
img.innerHTML = Image.message;
loc.appendChild(img)
});
}
document.addEventListener('DOMContentLoaded', () => {
ceo()
})