【问题标题】:Javascript not working - changing image to text on clickJavascript 不起作用 - 单击时将图像更改为文本
【发布时间】:2014-06-20 12:52:31
【问题描述】:

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

应用程序.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()
})

【问题讨论】:

  • 您能否详细说明“不工作”?是否发生任何错误?选择器是否找到了所需的元素?元素是否可能被替换?
  • 本题请直接出示相关代码。
  • 我更新了帖子。不工作是指 - 当我点击其中一个图标时,它不会变为文本。
  • 请显示呈现的 HTML 而不是服务器端代码。

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


【解决方案1】:

如果之前可以正常工作,则问题可能是 Turbolinks 的问题:

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

【讨论】:

  • 无法正常工作。它在点击时将图像更改为文本,但同时为页面上的所有 cmets 更改它。
猜你喜欢
  • 1970-01-01
  • 2012-10-07
  • 1970-01-01
  • 2021-01-12
  • 1970-01-01
  • 1970-01-01
  • 2019-11-13
  • 2021-06-02
  • 2015-06-27
相关资源
最近更新 更多