簡體   English   中英

當用戶在rails上重新加載頁面時如何隨機加載圖像

[英]how to randomly load an image when users reload the page on rails

我是新來的軌道。 當用戶重新加載頁面時,我試圖從 API 中獲取隨機圖像。

在 html 中,我有,

<%= image_tag id: 'login_gif', width: "370" %>

在 javascript 中,我有,

let loginGif = document.getElementById('login_gif')
fetch("https://aws.random.cat/meow")
  .then(res => res.json())
  .then(pic => { (loginGif.attr("src", pic.file ))})

我該怎么做呢? 感謝您提前提供任何幫助。

您使用了 jQuery .attr()方法...但是loginGif不是 jQuery object。

試試.then(pic => { (loginGif.src = pic.file)})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM