gpt4 book ai didi

javascript - 如何设置 DIV 背景图像 css jquery?

转载 作者:太空宇宙 更新时间:2023-11-04 02:31:10 26 4
gpt4 key购买 nike

座右铭是设置/更新(ajax化)DIV 背景图像。

        $.getJSON("https://itunes.apple.com/search?term=" + searchTerm + '&limit=1' + '&callback=?', 
function(data) {
$.each(data.results, function() {
var art = this.artworkUrl100;
$('.photo').parent().css('background-image', 'url(' + art + ')');
}
}

在 HTML 上我有这个:

<div id="results" class="photo"></div>

最佳答案

您正在将背景图像设置为父元素而不是 .photo div。

试试这个:

$.getJSON("https://itunes.apple.com/search?term=" + searchTerm + '&limit=1' + '&callback=?', 
function(data) {
$.each(data.results, function() {
var art = this.artworkUrl100;
$('.photo').css('background-image', 'url(' + art + ')');
})
});

如果返回多个 data.results,则由于您正在使用的 $.each 循环,只有最后一个用于背景图像。如果您在 JSON 调用中保持 limit=1,那么这将不是问题。

关于javascript - 如何设置 DIV 背景图像 css jquery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36488765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com