-1
私はconsole.logに値を表示することができましたが、WordPress REST APIから抽出した値をCSS背景画像を変更するために使用すると、定義されていない値。私は静的なHTMLを作成しようとしているが、自分のサイトにブログのおすすめ画像を表示しようとしている。WordPress REST API(JSON)でCSS背景画像を変更
JS:
var newImage;
var postRequest = new XMLHttpRequest();
postRequest.open('GET','http://method.com.sg/post/wp-json/wp/v2/posts');
postRequest.onload = function(){
var ourData = JSON.parse(postRequest.responseText);
newImage = ourData[0].better_featured_image.source_url;
console.log(newImage); //Will output the hyperlink
};
alert(newImage); // Output Undefined
postRequest.send();
//Unable to work
$('.container.withBg.post').css('background-image', 'url('+newImage+')');
ああ動作します!ダミー関数は私の人生を救った。ありがとうございます。 –