使用JQuery在Ruby on Rails中更改图像(Use JQuery for change a image in Ruby on Rails)

编程入门 行业动态 更新时间:2024-10-11 11:15:35
使用JQuery在Ruby on Rails中更改图像(Use JQuery for change a image in Ruby on Rails)

我在使用JQuery更改图像时遇到问题。

来自我的hmtl.erb的片段:

<%= image_tag('empty-screen.png', style:'width: 100%', class:'img-responsive center-block', id:'screen-d') %>

来自我的javascript片段:

$("#collapseOne").on('show.bs.collapse',function () { $('#screen-d').attr("src","#{asset_path('empty-screen.png')}"); });

真正的问题是我不太清楚Rails如何使用资产管道。 我相信使用asset_path会起作用,但是使用该代码我只是将src更改为#{asset_path('empty-screen.png')} 。 所以我真的不知道该怎么做。

I'm having problems to change an image using JQuery.

The fragment from my hmtl.erb:

<%= image_tag('empty-screen.png', style:'width: 100%', class:'img-responsive center-block', id:'screen-d') %>

Fragment from my javascript:

$("#collapseOne").on('show.bs.collapse',function () { $('#screen-d').attr("src","#{asset_path('empty-screen.png')}"); });

The real problem is that I don't know very well how Rails uses the asset pipeline. I believed that using asset_path would work, but with that code I was just changing the src to #{asset_path('empty-screen.png')}. So I don't really know what to do.

最满意答案

尝试

$("#collapseOne").on('show.bs.collapse',function () { $('#screen-d').attr("src","<%= asset_path('empty-screen.png') %>"); });

并确保在.js文件的末尾添加.erb

Try

$("#collapseOne").on('show.bs.collapse',function () { $('#screen-d').attr("src","<%= asset_path('empty-screen.png') %>"); });

And make sure to add a .erb at the end of your .js file

更多推荐

本文发布于:2023-07-27 03:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1284834.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   Ruby   JQuery   Rails   image

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!