简体   繁体   中英

Use JQuery for change a image in Ruby on Rails

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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