简体   繁体   English

如何在准备好的文档上使用Ajax加载图像

[英]how to load image using ajax on document ready

$(document).ready(function() {
initfun();
console.log("initalized !");
});

function initfun() {
var basepath = document.location.href + "/wp-content/themes/breviter/assets/tmbdapicalls.py";
$.ajax({
    type: "get",
    url: "http://127.0.0.1:5000/guestSuggestion",
    datatype: "json",
    success: function(response) {
        var output = response;
        //console.log(JSON.stringify(output));
        var json = JSON.parse(output);
        var backpostimage = "https://image.tmdb.org/t/p/w600" + json["results"][0]["backdrop_path"];
        console.log(backpostimage);
        document.getElementById("page-canvas").style = 'background-image: ' + backpostimage + ';background-repeat: no-repeat; background-size:100% 100%;'

    }
})
}

I am using the above code to set the css background of an element, but for some reason I cannot set the background-image paramater, other paramaters are being set successfully as shown below 我正在使用上面的代码设置元素的css背景,但是由于某些原因我无法设置background-image参数,其他参数正在成功设置,如下所示

<div id="page-canvas" class="page-wrapper" style="background-repeat: no-repeat; background-size: 100% 100%;">

You have not passed image path to url() function in background-image 您尚未将图片路径传递给background-image url()函数

document.getElementById("page-canvas").style = 'background-image: ' + backpostimage + ';background-repeat: no-repeat; background-size:100% 100%;'

Correct is 正确的是

document.getElementById("page-canvas").style = 'background-image: url(' + backpostimage + ');background-repeat: no-repeat; background-size:100% 100%;'

You should not be setting the whole string with style, just set the image 您不应该使用样式设置整个字符串,而只是设置图像

document.getElementById("page-canvas").style.backgroundImage = 'url("' + backpostimage + '")';

https://developer.mozilla.org/en-US/docs/Web/CSS/background-image https://developer.mozilla.org/zh-CN/docs/Web/CSS/background-image

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM