Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Rails jQuery使用';阅读更多&;少读';链接_Javascript_Jquery_Ruby On Rails - Fatal编程技术网

Javascript Rails jQuery使用';阅读更多&;少读';链接

Javascript Rails jQuery使用';阅读更多&;少读';链接,javascript,jquery,ruby-on-rails,Javascript,Jquery,Ruby On Rails,我正试图找到一种方法,让评论显示在“阅读更多”链接中,点击后不会刷新页面。下面的代码不会刷新页面,但如果单击任何单个链接,所有注释都将展开,而不仅仅是一条注释。我正在寻找一种好方法,可以将注释截断,然后在单击“阅读更多”链接而不刷新页面时只显示一条注释 <% @post.comments.order(created_at: :desc).each do |comment| %> <b><%= comment.user.first_name.capital

我正试图找到一种方法,让评论显示在“阅读更多”链接中,点击后不会刷新页面。下面的代码不会刷新页面,但如果单击任何单个链接,所有注释都将展开,而不仅仅是一条注释。我正在寻找一种好方法,可以将注释截断,然后在单击“阅读更多”链接而不刷新页面时只显示一条注释

  <% @post.comments.order(created_at: :desc).each do |comment| %>
    <b><%= comment.user.first_name.capitalize + " "%><%= comment.user.last_name.capitalize %></b>

    <% if comment.content.length > 100 %>
      <div class='textControl'><%= truncate(comment.content, length: 100) %></div>
      <div class='textControl' style='display:none;'><%= comment.content %></div>
      <%= link_to '...Read more', '', class: "read-more-#{comment.id} textControl" %>

      <script>
        $('.read-more-<%= comment.id %>').on('click', function(e) {
          e.preventDefault();
          $('.textControl').toggle();
        })
      </script>
    <% else %>
      <%= comment.content %>
    <% end %>

    [<%= link_to ' Edit', edit_group_post_comment_path(comment.post.group, comment.post, comment) %> |
    <%= link_to 'Delete ', group_post_comment_path(@group, @post, comment), 
                           method: :delete, data: { confirm: 'Are you sure you want to delete this comment?' } %>]
    <%= time_ago_in_words(comment.updated_at) %>                                   
    <br><br>
  <% end %>

100 %>
$('.read more-')。关于('单击',函数(e){
e、 预防默认值();
$('.textControl').toggle();
})
[ |
]



看,你不需要额外的隐藏类就可以做到这一点,就像你可以删除它一样,实际上,你不需要使用这些额外的行

<div class='textControl' style='display:none;'><%= comment.content %></div>
<%= link_to '...Read more', '', class: "read-more-#{comment.id} textControl" %>
a.morelink{
文字装饰:无;
大纲:无;
}
.Morespan内容{
显示:无;
}

大于100

与流行的观点相反,Lorem Ipsum不是简单的随机文本。它起源于公元前45年的一段古典拉丁文学,距今已有2000多年的历史。弗吉尼亚州汉普顿悉尼学院的拉丁语教授理查德·麦克林托克(Richard McClintock)从《洛伦·伊普斯姆》(Lorem Ipsum)一段中查找了一个更为晦涩的拉丁语单词,即“Concertetur”,并查阅了古典文学中对该词的引用,发现了该词无可置疑的来源。Lorem Ipsum来自西塞罗于公元前45年所著《德菲尼布斯·博诺勒姆和马洛勒姆》(善与恶的极端)的第1.10.32节和第1.10.33节。这本书是一本关于伦理学理论的论文,在文艺复兴时期非常流行。Lorem Ipsum的第一行“Lorem Ipsum dolor sit amet..”来自第1.10.32节中的一行

少于100

Lorem ipsum dolor sit amet

看,你可以不用额外的隐藏类来完成这项工作,比如你可以删除它,实际上,你不需要使用这些额外的行

<div class='textControl' style='display:none;'><%= comment.content %></div>
<%= link_to '...Read more', '', class: "read-more-#{comment.id} textControl" %>
a.morelink{
文字装饰:无;
大纲:无;
}
.Morespan内容{
显示:无;
}

大于100

与流行的观点相反,Lorem Ipsum不是简单的随机文本。它起源于公元前45年的一段古典拉丁文学,距今已有2000多年的历史。弗吉尼亚州汉普顿悉尼学院的拉丁语教授理查德·麦克林托克(Richard McClintock)从《洛伦·伊普斯姆》(Lorem Ipsum)一段中查找了一个更为晦涩的拉丁语单词,即“Concertetur”,并查阅了古典文学中对该词的引用,发现了该词无可置疑的来源。Lorem Ipsum来自西塞罗于公元前45年所著《德菲尼布斯·博诺勒姆和马洛勒姆》(善与恶的极端)的第1.10.32节和第1.10.33节。这本书是一本关于伦理学理论的论文,在文艺复兴时期非常流行。Lorem Ipsum的第一行“Lorem Ipsum dolor sit amet..”来自第1.10.32节中的一行

少于100

Lorem ipsum dolor sit amet

如果内容中有
\n
,则不会显示该内容。您可以添加
空白:预换行textControl
样式中的code>来处理此情况。如果内容中有
\n
,则不会显示它。您可以添加
空白:预换行textControl
样式中的code>来处理此情况。
<% if comment.content.length > 100 %>