简体   繁体   English

当用户在rails上重新加载页面时如何随机加载图像

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

I am new to rails.我是新来的轨道。 I am trying to fetch a randomly image from an API when users reload the page.当用户重新加载页面时,我试图从 API 中获取随机图像。

In the html, I have,在 html 中,我有,

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

In javascript, I have,在 javascript 中,我有,

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

How do I do this?我该怎么做呢? Thanks for any help in advance.感谢您提前提供任何帮助。

You used the jQuery .attr() method... But loginGif is not a jQuery object.您使用了 jQuery .attr()方法...但是loginGif不是 jQuery object。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM