简体   繁体   English

使用JQuery在Ruby on Rails中更改图像

[英]Use JQuery for change a image in Ruby on Rails

I'm having problems to change an image using JQuery. 我在使用JQuery更改图像时遇到问题。

The fragment from my hmtl.erb : 我的hmtl.erb的片段:

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

Fragment from my javascript : 来自我的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. 真正的问题是我不太了解Rails如何使用资产管道。 I believed that using asset_path would work, but with that code I was just changing the src to #{asset_path('empty-screen.png')} . 我相信使用asset_path会起作用,但是使用该代码,我只是将src更改为#{asset_path('empty-screen.png')} So I don't really know what to do. 所以我真的不知道该怎么办。

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 并确保在.js文件的末尾添加.erb

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

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