无法选择link_to标记作为jquery选择器Rails [英] Can't select link_to tag as jquery selector Rails
问题描述
我是初学者Rails开发人员.我得到了带有"comments_link"类的link_to标记
I am a beginner rails dev. I got this link_to tag with the class of "comments_link"
%h2= link_to pluralize(@recipe.comments.count, "Comment"), "#", class: "comments_link , btn btn-success"
现在,在我写的 application.js 中
$(document).ready(function(){
$(".comments_link").click(function(){
$(".section").toggle();
});
})
此代码的问题,无论我为click事件选择什么元素,它都有效.但是当我使用.comments_link
类时,它不起作用.为什么会这样呢?
The problem with this code, whatever element i choose for click event, it works. But when i use .comments_link
class, it doesn't work. Why is this happening?
除link_to
标记外的所有内容均可用作事件侦听器.我需要添加一些代码吗?
Anything but link_to
tags work as event listener. Do i need to add some sort of code to it?
顺便说一句,我只是做了相反的尝试,但还是奏效了.当我单击该部分时,它会切换链接.但是链接不能是事件侦听器.
Btw i did the reverse just to try and it worked. When i click on the section, it toggles the link. But the link cant be event listener.
$(document).ready(function(){
$(".section").click(function(){
$(".comments_link").toggle();
});
}); <-- this one worked. I cant use ".comments_link" as event listener.
推荐答案
您在类名中有一个逗号,我认为这会使该类名无效.检查开发人员工具以检查元素以查看.comment_link
是否实际存在.如果不是,请删除逗号,然后重试
You have a comma in the class name which I assume would invalidate the class name. Check developer tools in order to inspect the element to see if .comment_link
actually exists. If not, remove the comma and try again
这篇关于无法选择link_to标记作为jquery选择器Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!