1

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

我确定这很简单,但我只是看不到它。

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

    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);
  }
}); 

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

任何帮助都感激不尽。

4

1 回答 1

0

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

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>
于 2017-03-18T20:25:13.320 回答