[英]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.