繁体   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