0

我好近!有点困惑......所以告诉我是否让事情过于复杂。

我正在使用公共Giphy API来拉出一个随机 gif(在下面的示例中为 tag=dog),并且希望在每次单击按钮时循环浏览新的 gif。

在下面的示例中,我让控制台记录“成功”,我只是不太清楚如何将新的随机 URL 设置为源。

任何帮助表示赞赏,在此先感谢!

HTML:

<div class="iframe">
  <iframe src="https://giphy.com/embed/26FmRLBRZfpMNwWdy" id="giphy-embed">
  </iframe>
</div>

<button type='button' class="btn btn-default" id="new"> New GIF
</button>

JS/jQuery:

$("#new").click(function () {
  var gif = $('#giphy-embed').attr('src', function(newGIF){ //This is where it will go!
  $.ajax ({
    url: "//api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=dog",
    type: "GET",
    success: function(response) {

      console.log("success");    //This is what I get, just need to set the new URL as the src
    }, 
    error: function(e) {
      console.log("uh oh");
      }
    });
  });

});
4

1 回答 1

0

尝试将 iframe src 作为成功函数的一部分进行更新。

$("#new").click(function () {
 //This is where it will go!
  var imgSrc;
  $.ajax ({
    url: "//api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=dog",
    type: "GET",
    success: function(response) {
      // console.log(response);
      imgSrc = response.data.image_url;
      $("#giphy-embed").attr("src", imgSrc);
      return false;
    }, 
    error: function(e) {
      console.log("uh oh");
      }
    });
});
于 2017-06-19T03:43:30.897 回答