Rails 3.1 CoffeeScript / JQuery - 如何更改div中的图像的来源? [英] Rails 3.1 CoffeeScript/JQuery - How to change the source of an image in a div?
本文介绍了Rails 3.1 CoffeeScript / JQuery - 如何更改div中的图像的来源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想更改使用jquery的链接的图像。这是我试过的:
在我的Haml文件中:
.selected
= link_to image_tag('/ assets / unselected.png'),{:controller =>帖子::action => update,:selected => true},:id => selected_post,:remote => true,:method => :put
在我的posts_controller的一些东西后,这里是update.js.coffee,用一个新的名为selected.png的链接改变图像:
$(selected_post)。attr'src' ,'/assets/selected.png'
我不工作,
alert $(selected_post)。attr'src'
它发送未定义。
我想我错过了一些东西。
$ b $p>
#
是 id
属性的css快捷方式。所以这应该工作: $(#selected_post)。attr'src','/assets/selected.png'
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.
解决方案
#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'
这篇关于Rails 3.1 CoffeeScript / JQuery - 如何更改div中的图像的来源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文