gpt4 book ai didi

javascript - 单击弹出窗口显示错误数据

转载 作者:行者123 更新时间:2023-11-28 04:51:44 24 4
gpt4 key购买 nike

我正在制作一个供自己使用的电影时间表页面,但我遇到了问题。我想让页面弹出我点击的电影的信息,但它总是显示列表中最后一部电影的信息。

我确信这很简单,但我就是不明白。

我用来获取所需信息的代码:

    var settings = {
"async": true,
"crossDomain": true,
"url": ("https://api.themoviedb.org/3/search/movie?api_key=e8a6a870421f5cc13e775873bfe1cad8&language=bg&query=")+encodeURIComponent(fn),
"method": "GET",
"headers": {},
"data": "{}"
}
$.ajax(settings).done(function (response) {
var plakat = response.results[0].poster_path;
window.movieId = response.results[0].id;
window.title = response.results[0].title;
window.poster = "https://image.tmdb.org/t/p/w92/"+plakat;
window.overview = response.results[0].overview;
console.log(response.results[0].title);
var k;
for(k=0; k< ttest.length; k++){
document.getElementById('myPopup').innerHTML = ("<img align='left' src="+window.poster+"><h2>"+window.title+"</h2><br>"+window.overview);
}
});

然后我想循环所有结果,以便我可以更改弹出窗口中显示的数据,但它似乎不起作用。

任何帮助将不胜感激。

最佳答案

这将显示每部电影的缩略图。单击缩略图时,将显示标题和说明(如果有):

JS

var fn = "snow white";
var settings = {
"async": true,
"crossDomain": true,
"url": ("https://api.themoviedb.org/3/search/movie?api_key=e8a6a870421f5cc13e775873bfe1cad8&language=bg&query=")+encodeURIComponent(fn),
"method": "GET",
"headers": {},
"data": "{}"
}

$.ajax(settings).done(function (response) {

for(var i = 0; i < response.results.length; i++) {
var obj = response.results[i];

var plakat = obj.poster_path;
var movieId = obj.id;

var title = obj.title.replace(/'/g, "&#39;");
var title = title.replace(/"/g, '\\"');

if (plakat !== null) {
var poster = "https://image.tmdb.org/t/p/w92/"+plakat;
} else {
var poster = "";
}

var overview = obj.overview.replace(/'/g, "&#39;");
var overview = overview.replace(/"/g, '\\"');

$("#movieList").append("<a href='javascript:showInfo("+'"'+title+'"'+","+'"'+overview+'"'+");'><img src='"+poster+"' height='102' width='82' style='padding:5px;'></a>");

}
});


function showInfo(title,overview){
document.getElementById('myPopup').innerHTML = "<b>"+title+"</b>"+" <br>"+overview;
}

HTML

<div id="movieList" align="left"></div>
<div id="myPopup"></div>

关于javascript - 单击弹出窗口显示错误数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42868058/

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