gpt4 book ai didi

javascript - 如何: JSON image URL display as actual image

转载 作者:行者123 更新时间:2023-11-30 00:19:27 25 4
gpt4 key购买 nike

我下面的 JS 代码抓取 JSON 对象并将它们打印在网页上,但只是文本。 IE。这将打印图像 URL(实际文本)与显示实际图像。我怎样才能更改下面的代码...在代码中的某处添加 img src= 以在页面上显示实际图像?

	function onShowtimesLoaded(data){
var movieArray = data.results[0].movie;
movieArray.forEach(function(el, index) {

var elm = document.createElement('div');
elm.setAttribute('id','movie_'+parseInt(index+1));
elm.appendChild(document.createElement('br'));
elm.appendChild(document.createTextNode("Movie Title: " + el.movieTitle));
elm.appendChild(document.createElement('br'));
elm.appendChild(document.createTextNode("Movie Poster: " + el.poster));
elm.appendChild(document.createElement('br'));
elm.appendChild(document.createTextNode("Movie Show Times: " ));
var showtimes = el.showtimes.forEach(function(element, index) {
elm.appendChild(document.createTextNode(" "+element.time+" "));
});
document.getElementById("MovieContainer").appendChild(elm);
});
}
function callAPI() {
var pincode = document.getElementById("pincode").value;
(function(){
//note the "onShowtimesLoaded" at the end
var src = 'closesttheater-'+pincode+'.json?format=json&callback=onShowtimesLoaded';
script = document.createElement('SCRIPT');
script.src = src;
document.head.appendChild(script);
})();
}

最佳答案

这听起来不错吧?

<script>
function onShowtimesLoaded(data){
var movieArray = data.results[0].movie;
movieArray.forEach(function(el, index) {

var elm = document.createElement('div');
elm.setAttribute('id','movie_'+parseInt(index+1));
elm.appendChild(document.createElement('br'));
elm.appendChild(document.createTextNode("Movie Title: " + el.movieTitle));
elm.appendChild(document.createElement('br'));
var tmp_img = document.createElement('img');
tmp_img.src = el.poster;
elm.appendChild(document.createTextNode("Movie Poster: " ));
elm.appendChild(tmp_img);
elm.appendChild(document.createElement('br'));
elm.appendChild(document.createTextNode("Movie Show Times: " ));
var showtimes = el.showtimes.forEach(function(element, index) {
elm.appendChild(document.createTextNode(" "+element.time+" "));
});
document.getElementById("MovieContainer").appendChild(elm);
});
}
function callAPI() {
var pincode = document.getElementById("pincode").value;
(function(){
//note the "onShowtimesLoaded" at the end
var src = 'closesttheater-'+pincode+'.json?format=json&callback=onShowtimesLoaded';
script = document.createElement('SCRIPT');
script.src = src;
document.head.appendChild(script);
})();
}
</script>

关于javascript - 如何: JSON image URL display as actual image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33725792/

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