我尝试使用样式标记在JavaScript中引用的div上应用背景图片。我确定它与图像变量周围的引用有关。任何帮助将不胜感激。
function appendData () {
var image = "http://domain.com/image.jpg";
var html = '<div class="itemImage" style="background-image:url(' + image + ');"></div>';
$('.container').append(html);
}
答案 0 :(得分:2)
我会考虑稍微改变你的方法。当然,您可以将html附加到容器中,或者您可以更改itemImage的属性,而无需每次都重写它。
如果将itemImage div构建到该容器中,则可以执行此操作
function appendData () {
var image = "http://domain.com/image.jpg";
$(".itemImage").css("background-image", image);
}
答案 1 :(得分:1)
function appendData () {
var image = "http://domain.com/image.jpg";
var html = '<div class="itemImage" style="background-image:url(\'' + image + '\');"></div>';
$('.container').append(html);
}