Javascript无法正常工作 - 点击时将图片更改为文字

时间:2014-05-04 02:08:35

标签: ruby-on-rails ruby-on-rails-4

我最近问过question如何在点击时将2个图标更改为文字。解决方案工作了一段时间,但现在它无法正常工作。还有另一种方法可以做到这一点,还是代码中的问题?我经常遇到其他脚本的问题。

的application.js

//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require jquery.ui.all
//= require jquery.turbolinks
//= require jquery_nested_form
//= require_tree .

show.html.erb

<div class="thumbsup">
<%= link_to image_tag('othericons/thumbsup_off.PNG', height: '20', width: '20', like_post_comment_path(comment.post_id, comment), method: :put, :remote => true %>
</div>

<div class="thumbsdown">
<%= link_to image_tag('othericons/thumbsdown_off.PNG', height: '20', width: '20', dislike_post_comment_path(comment.post_id, comment), method: :put, :remote => true %>
</div>

html输出

<div class="thumbsup">
    <a data-method="put" data-remote="true" href="/posts/1/comments/4/like" rel="nofollow">
        <img alt="Thumbsup off" height="21" src="/assets/othericons/thumbsup_off.PNG" width="20" />
    </a>
</div>

<div class="thumbsdown">
    <a data-method="put" data-remote="true" href="/posts/1/comments/4/dislike" rel="nofollow">
        <img alt="Thumbsdown off" height="21" src="/assets/othericons/thumbsdown_off.PNG" width="20" />
    </a>
</div>

icons.js

$('.thumbsdown a, .thumbsup a').on('click', function() 
{
    $('.thumbsdown').before('You voted')
    $('.thumbsdown, .thumbsup').remove()
})

1 个答案:

答案 0 :(得分:0)

如果之前有效,问题很可能是Turbolinks

的问题
$(document).on("click", ".thumbsdown a, .thumbsup a", function() {
    $('.thumbsdown').before('You voted')
    $('.thumbsdown, .thumbsup').remove()
});