Rails 3.1 CoffeeScript/JQuery - How to change the source of an image in a div?

有些话、适合烂在心里 提交于 2019-12-24 12:51:44

问题


I would like to change the image of a link with jquery. This is what I tried :

In my Haml file :

.selected
  = link_to image_tag('/assets/unselected.png'), {:controller => Posts, :action => "update", :selected => true}, :id => "selected_post", :remote => true, :method => :put

After some stuff in my posts_controller, here is the update.js.coffee where I tried to change the image of the link with a new one called selected.png :

$("selected_post").attr 'src', '/assets/selected.png'

I does not work and if I call an alert with :

alert $("selected_post").attr 'src'

It sends "undefined".

I think I am missing something.


回答1:


#selected_post should be your selector

# is the css shortcut for the id attribute. So this should work:

$("#selected_post").attr 'src', '/assets/selected.png'


来源:https://stackoverflow.com/questions/8555528/rails-3-1-coffeescript-jquery-how-to-change-the-source-of-an-image-in-a-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!