使用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
更多推荐
发布评论