2017-10-11 59 views
0
$(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%;' 

    } 
}) 
} 

我使用上面的代碼來設置元件的CSS背景,但由於某種原因我無法設置背景圖像paramater Ajax加載圖像,其他PARAMATERS被成功地作爲設定下面如何使用關於文檔準備

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

在'網址圍住圖像()' – Niladri

回答

3

顯示您還沒有background-image

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

正確傳遞圖像路徑url()功能

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

接受的答案,如果幫你 –

4

你不應該設置整串的風格,只是設置圖像

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

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

+0

根據您提供的鏈接,您的答案不正確。 正確的是'document.body.style.backgroundImage =「url('img_tree.png')」; '檢查此鏈接https://www.w3schools.com/jsref/prop_style_backgroundimage.asp –

+0

咖啡沒有踢....大聲笑 – epascarello