设置]的背景图像> 在JavaScript中[关闭] 关闭。 这个问题是not reproducible or was caused by typos。它当前不接受答案。 是否要改善此问题? Update the question,因此堆栈溢出为on-topic。 已关闭1分钟前 。 我正在尝试在Javascript中设置background-image的<div>属性,但是图像不会出现。高度为100像素,我可以看到其下方的内容被下推,但是这里没有图像。 这是我的代码: var bg = document.createElement("div"); $.getJSON("https://www.khanacademy.org/api/internal/user/profile?kaid=kaid_783146703471696540623752&callback=?", function(data) { bg.style.backgroundImage = 'url("https://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Basilica_of_Saint_Achilles%2C_Florina%2C_Greece.jpg/1280px-Basilica_of_Saint_Achilles%2C_Florina%2C_Greece.jpg");'; bg.style.height = "100px"; statsDiv.appendChild(bg); //some other stuff that uses the JSON that doesn't matter }); 我知道<div>是那里 ,因为它显示了元素之间的100px间距,但是没有图像,并且$.getJSON()有效。 解决方案: ] 我只需要删除分号: 之前:bg.style.backgroundImage = 'url("https://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Basilica_of_Saint_Achilles%2C_Florina%2C_Greece.jpg/1280px-Basilica_of_Saint_Achilles%2C_Florina%2C_Greece.jpg");'; 之后:bg.style.backgroundImage = 'url("https://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Basilica_of_Saint_Achilles%2C_Florina%2C_Greece.jpg/1280px-Basilica_of_Saint_Achilles%2C_Florina%2C_Greece.jpg")'; 我正在尝试在Javascript中设置 的背景图像属性,但是图像不会出现。高度为100像素,我可以看到其下方的内容被下推,但是这里是...

问题描述 投票:0回答:2

我正在尝试在Javascript中设置background-image<div>属性,但是图像不会出现。高度为100像素,我可以看到其下方的内容被下推,但是这里没有图像。

javascript html css background-image getjson
2个回答
-2
投票

到目前为止看起来不错。新的div必须附加到DOM。


-3
投票

这对我有用:

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