为什么我不能在fetch中添加图像src属性。然后呢? [关闭]

问题描述 投票:0回答:1
dropdown.addEventListener('change', event => {
fetch(`https://dog.ceo/api/breed/${dropdown.value}/images/random`)
    .then(response => {
        if (response.ok) {
            return response.json()
        }
        throw new Error('Bad HTTP')
    })
    .then(jsonData => {
        let image = document.createElement('image')
        let src = jsonData.message
        image.src = src;
        image.className = 'newClass'
        console.log(src)
        console.log(image)
        result.appendChild(image)
    })

})

console.log image

图像是我的consol.logs,类添加得很好,但src失败了,有人可以解释我做错了什么:(?

javascript image attributes fetch src
1个回答
2
投票

问题是创建"image"而不是"img".image标签将创建: The obsolete Image element而不是<img>

根据MDN

创建没有src属性的元素会导致HTMLElement对象具有本地元素名称"image"

qazxsw poi没有任何qazxsw poi属性所以qazxsw poi属性未设置

Incorrect

HTMLElement

当您创建src时,这将不会创建ImageElement

Correct

src
© www.soinside.com 2019 - 2024. All rights reserved.