我想用 jquery 更改链接的图像。这是我尝试过的:
在我的 Haml 文件中:
.selected
= link_to image_tag('/assets/unselected.png'), {:controller => Posts, :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'
它发送“未定义”。
我想我错过了一些东西。
最佳答案
#selected_post
应该是您的选择器
#
是 id
属性的 CSS 快捷方式。所以这应该有效:
$("#selected_post").attr 'src', '/assets/selected.png'
关于jquery - Rails 3.1 CoffeeScript/JQuery - 如何更改 div 中图像的来源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8555528/