Ruby on rails Rails4-js更改单击问题上的所有注释
我已经为这个(可能很简单的问题)挣扎了很多天,这里似乎没有人知道答案。我已经询问了一个和另一个,现在我可以在单击时将图标更改为文本 问题是,当我有一个评论列表并单击其中一个图标(向上/向下)时,它会将页面上所有评论的所有图标更改为“已投票!” 评论索引页Ruby on rails Rails4-js更改单击问题上的所有注释,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我已经为这个(可能很简单的问题)挣扎了很多天,这里似乎没有人知道答案。我已经询问了一个和另一个,现在我可以在单击时将图标更改为文本 问题是,当我有一个评论列表并单击其中一个图标(向上/向下)时,它会将页面上所有评论的所有图标更改为“已投票!” 评论索引页 <% @comments.each do |comment| %> <% comment.content %> div class="thumbsup"> <%= link_to
<% @comments.each do |comment| %>
<% comment.content %>
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>
<% end %>
div class=“thumbsup”>
正确%>
正确%>
让我们看看代码上的内容
$('.thumbsdown a, .thumbsup a').on('click', function()
{
$('.thumbsdown').before('You voted')
$('.thumbsdown, .thumbsup').remove()
})
$('.thumbsdown.thumbsup')
是一个jQuery函数,它使用选择器.thumbsdown
返回所有元素。并且也移除了所有这些。
您应该让jQuery知道您要删除或更改的示例。
例如,它可以是:
$('.thumbsdown a, .thumbsup a').on('click', function()
{
var parent = $(this).closest('li'); # would be better to use some class instead
$('.thumbsdown', parent).before('You voted'); # select elements in context of the parent
$('.thumbsdown, .thumbsup', parent).remove(); # select elements in context of the parent
})
添加:
jQuery.on()
要求在函数调用时至少有一个元素可用,以便正常工作。在这种情况下,.thumbsdown a、.thumbsup a
。
如果使用资产管道,则在加载主体之前加载普通脚本。您可能希望将示例代码放入
$(function(){
/* code here */
})
在加载DOM后加载它,或将其修改为
$(document).on('click', '.thumbsdown a, .thumbsup a', function() {
让我们看看代码
$('.thumbsdown a, .thumbsup a').on('click', function()
{
$('.thumbsdown').before('You voted')
$('.thumbsdown, .thumbsup').remove()
})
$('.thumbsdown.thumbsup')
是一个jQuery函数,它使用选择器.thumbsdown
返回所有元素。并且也移除了所有这些。
您应该让jQuery知道您要删除或更改的示例。
例如,它可以是:
$('.thumbsdown a, .thumbsup a').on('click', function()
{
var parent = $(this).closest('li'); # would be better to use some class instead
$('.thumbsdown', parent).before('You voted'); # select elements in context of the parent
$('.thumbsdown, .thumbsup', parent).remove(); # select elements in context of the parent
})
添加:
jQuery.on()
要求在函数调用时至少有一个元素可用,以便正常工作。在这种情况下,.thumbsdown a、.thumbsup a
。
如果使用资产管道,则在加载主体之前加载普通脚本。您可能希望将示例代码放入
$(function(){
/* code here */
})
在加载DOM后加载它,或将其修改为
$(document).on('click', '.thumbsdown a, .thumbsup a', function() {
让我们看看代码
$('.thumbsdown a, .thumbsup a').on('click', function()
{
$('.thumbsdown').before('You voted')
$('.thumbsdown, .thumbsup').remove()
})
$('.thumbsdown.thumbsup')
是一个jQuery函数,它使用选择器.thumbsdown
返回所有元素。并且也移除了所有这些。
您应该让jQuery知道您要删除或更改的示例。
例如,它可以是:
$('.thumbsdown a, .thumbsup a').on('click', function()
{
var parent = $(this).closest('li'); # would be better to use some class instead
$('.thumbsdown', parent).before('You voted'); # select elements in context of the parent
$('.thumbsdown, .thumbsup', parent).remove(); # select elements in context of the parent
})
添加:
jQuery.on()
要求在函数调用时至少有一个元素可用,以便正常工作。在这种情况下,.thumbsdown a、.thumbsup a
。
如果使用资产管道,则在加载主体之前加载普通脚本。您可能希望将示例代码放入
$(function(){
/* code here */
})
在加载DOM后加载它,或将其修改为
$(document).on('click', '.thumbsdown a, .thumbsup a', function() {
让我们看看代码
$('.thumbsdown a, .thumbsup a').on('click', function()
{
$('.thumbsdown').before('You voted')
$('.thumbsdown, .thumbsup').remove()
})
$('.thumbsdown.thumbsup')
是一个jQuery函数,它使用选择器.thumbsdown
返回所有元素。并且也移除了所有这些。
您应该让jQuery知道您要删除或更改的示例。
例如,它可以是:
$('.thumbsdown a, .thumbsup a').on('click', function()
{
var parent = $(this).closest('li'); # would be better to use some class instead
$('.thumbsdown', parent).before('You voted'); # select elements in context of the parent
$('.thumbsdown, .thumbsup', parent).remove(); # select elements in context of the parent
})
添加:
jQuery.on()
要求在函数调用时至少有一个元素可用,以便正常工作。在这种情况下,.thumbsdown a、.thumbsup a
。
如果使用资产管道,则在加载主体之前加载普通脚本。您可能希望将示例代码放入
$(function(){
/* code here */
})
在加载DOM后加载它,或将其修改为
$(document).on('click', '.thumbsdown a, .thumbsup a', function() {
不起作用。现在,我又回到了我在链接问题中遇到的同样问题——图标不会在单击时变为文本。请在问题中添加注释列表的标记。我相信我的解决方案将与正确的选择器一起工作。这是一个非常简单的问题。我在前面的问题中列出了所有相关的内容。你认为问题可能出在rails中的gems上吗?有些解决方案会工作一段时间,然后就停止正常工作。您的标记看起来不太好。您生成了大量文本和
div
s,但没有任何语义。用一些
将每个注释包装到循环中,并用最近('.comment')
修改我的示例。当我将其直接放在页面上时,它会起作用。当我把它放入/assets时,它不起作用。所以,问题在于资产管道,而不是您的代码。不起作用。现在,我又回到了我在链接问题中遇到的同样问题——图标不会在单击时变为文本。请在问题中添加注释列表的标记。我相信我的解决方案将与正确的选择器一起工作。这是一个非常简单的问题。我在前面的问题中列出了所有相关的内容。你认为问题可能出在rails中的gems上吗?有些解决方案会工作一段时间,然后就停止正常工作。您的标记看起来不太好。您生成了大量文本和div
s,但没有任何语义。用一些
将每个注释包装到循环中,并用最近('.comment')
修改我的示例。当我将其直接放在页面上时,它会起作用。当我把它放入/assets时,它不起作用。所以,问题在于资产管道,而不是您的代码。不起作用。现在,我又回到了我在链接问题中遇到的同样问题——图标不会在单击时变为文本。请在问题中添加注释列表的标记。我相信我的解决方案将与正确的选择器一起工作。这是一个非常简单的问题。我在前面的问题中列出了所有相关的内容。你认为问题可能出在rails中的gems上吗?有些解决方案会工作一段时间,然后就停止正常工作。您的标记看起来不太好。您生成了大量文本和div
s,但没有任何语义。用一些
将每个注释包装到循环中,并用最近('.comment')
修改我的示例。当我将其直接放在页面上时,它会起作用。当我把它放入/assets时,它不起作用。所以,问题在于资产管道,而不是您的代码。不起作用。现在,我又回到了我在链接问题中遇到的同样问题-图标不会在单击时变为文本。请添加您的通信列表的标记