Javascript 不起作用 - 单击时将图像更改为文本 [英] Javascript not working - changing image to text on click

查看:28
本文介绍了Javascript 不起作用 - 单击时将图像更改为文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了一个问题 最近关于如何在单击时将 2 个图标更改为文本.该解决方案工作了一段时间,但现在不起作用.有没有其他方法可以做到,或者是代码中的问题?我经常遇到其他脚本的问题.

application.js

//= 需要 jquery//= 需要 jquery_ujs//= 需要引导程序//= 需要 jquery.ui.all//= 需要 jquery.turbolinks//= 需要 jquery_nested_form//= require_tree .

show.html.erb

<%= 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 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 %>

html 输出

<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 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>

icons.js

$('.thumbsdown a, .thumbsup a').on('click', function(){$('.thumbsdown').before('你投票')$('.thumbsdown, .thumbsup').remove()})

解决方案

如果之前可以使用,则问题很可能是 Turbolinks:

$(document).on("click", ".thumbsdown a, .thumbsup a", function() {$('.thumbsdown').before('你投票')$('.thumbsdown, .thumbsup').remove()});

I've asked a question recently on how to change 2 icons to text on click. The solution worked for a while, but now it's not working. Is there another way to do it or is the problem in the code? I face the problem with other scripts very often.

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 output

<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()
})

解决方案

If it was working before, the problem is likely an issue with Turbolinks:

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

这篇关于Javascript 不起作用 - 单击时将图像更改为文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆