【问题标题】:how to randomly load an image when users reload the page on rails当用户在rails上重新加载页面时如何随机加载图像
【发布时间】:2021-03-14 04:26:31
【问题描述】:

我是 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 ))})

我该怎么做?感谢您提前提供任何帮助。

【问题讨论】:

标签: javascript ruby-on-rails image random fetch


【解决方案1】:

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

试试.then(pic =&gt; { (loginGif.src = pic.file)})

【讨论】:

  • 您好,感谢您的帮助。我厌倦了解决方案。页面上没有加载任何图像。
猜你喜欢
  • 2016-05-15
  • 2014-01-08
  • 2017-02-20
  • 1970-01-01
  • 2021-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多