Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.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 5:使用read more&;隐藏链接_Javascript_Jquery_Html_Ruby On Rails - Fatal编程技术网

Javascript Rails 5:使用read more&;隐藏链接

Javascript Rails 5:使用read more&;隐藏链接,javascript,jquery,html,ruby-on-rails,Javascript,Jquery,Html,Ruby On Rails,我使用下面的代码来截断一些文本。正如你在下面看到的,一切都很好。目前,readmore和Hide链接始终出现。是否有方法在未单击“阅读更多”时隐藏“隐藏”链接,并在单击“阅读更多”链接时显示“隐藏”链接 <% @products.each do |product| %> <% if product.description.size > 100 %> <span class="truncated-paragraph-<%= product.id %&g

我使用下面的代码来截断一些文本。正如你在下面看到的,一切都很好。目前,
readmore
Hide
链接始终出现。是否有方法在未单击“阅读更多”时隐藏“隐藏”链接,并在单击“阅读更多”链接时显示“隐藏”链接

<% @products.each do |product| %>
<% if product.description.size > 100 %>
  <span class="truncated-paragraph-<%= product.id %>">
    <%= truncate product.description, length: 100 %>
  </span>
  <span class="normal-paragraph-<%= product.id %>" style="display: none;">
    <%= product.description %>
  </span>
  <a href="#" class="read-more-<%= product.id %>">Read More</a>
  <a href="#" class="read-less-<%= product.id %>">Hide</a>
<% else %>
  <%= product.description %>
<% end %>
<% end %>

<script type="text/javascript">
  $('[class^="read-more"]').click(function(element) {
    element.preventDefault()
    $(`.truncated-paragraph-${elId($(this))}`).hide()
    $(`.normal-paragraph-${elId($(this))}`).show()
  })

  $('[class^="read-less"]').click(function(element) {
    element.preventDefault()
    $(`.normal-paragraph-${elId($(this))}`).hide()
    $(`.truncated-paragraph-${elId($(this))}`).show()
  })

  function elId(element) {
    let elClassName = element.attr('class').split('-')
    return elClassName[elClassName.length - 1]
  }
</script>

Morbi非最新nec mi vulputate varius vel ac mi。事实上,这是一个很好的选择。整数nec est blandit,ullamcorper狮子座。。。
Morbi非最新nec mi vulputate varius vel ac mi。事实上,这是一个很好的选择。整数nec est blandit,ullamcorper leo iaculis,blandit dui。Suspendisse sem mauris,maximus Qus porta elementum,dolor发酵液。库拉比图·埃古斯塔斯·阿库·安特。向狮子座祈祷。静脉曲张门中的子宫内膜异位素。这是一个很好的例子。
萨皮安·费利斯,利奥·埃吉特的孕妇,猫的格言。这是一种精神上的骄傲,是一种高贵的气质。整数facilisis facilisis odio。征服者、非欧盟成员国、奥奇·库姆桑·胡斯托(orci tellus accumsan justo)、菲尼布斯·图尔皮斯·图尔皮斯(vitae finibus turpis)和托托(tortor)。这是一个很好的例子。花斑间细尾悬钩子。设施库拉比图直径和宽度。猫科动物中的Phasellus Placelat,孕妇velit at,pulvinar nulla。Mauris ut faucibus felis,vitae semper Elite。埃尼安·维尔·廷西登·利奥。不要把它当作一种快乐。梅塞纳斯·艾库利斯·波特·托托·布兰迪特。酒酿前酒,酒酿后酒。

您可以将段落封装在父div中,这样更容易编写交互代码:

$('.read more')。单击(函数(元素){
元素。preventDefault()
$(this).parent().find('.truncated段落').hide()
$(this).parent().find('.normal段落').show()
$(this.hide())
$(this.parent().find('.read less').show()
})
$('.read less')。单击(函数(元素){
元素。preventDefault()
$(this).parent().find('.truncated段落').show()
$(this).parent().find('.normal段落').hide()
$(this.hide())
$(this.parent().find('.read more').show()
})
函数elId(元素){
设elClassName=element.attr('class').split('-'))
返回elClassName[elClassName.length-1]
}
。隐藏{
显示:无;
}

Morbi非最新nec mi vulputate varius vel ac mi。事实上,这是一个很好的选择。整数nec est blandit,ullamcorper狮子座。。。
Morbi非最新nec mi vulputate varius vel ac mi。事实上,这是一个很好的选择。整数nec est blandit,ullamcorper leo iaculis,blandit dui。Suspendisse sem mauris,maximus Qus porta elementum,dolor发酵液。库拉比图·埃古斯塔斯·阿库·安特。向狮子座祈祷。静脉曲张门中的子宫内膜异位素。这是一个很好的例子。
萨皮安·费利斯,利奥·埃吉特的孕妇,猫的格言。这是一种精神上的骄傲,是一种高贵的气质。整数facilisis facilisis odio。征服者、非欧盟成员国、奥奇·库姆桑·胡斯托(orci tellus accumsan justo)、菲尼布斯·图尔皮斯·图尔皮斯(vitae finibus turpis)和托托(tortor)。这是一个很好的例子。花斑间细尾悬钩子。设施库拉比图直径和宽度。猫科动物中的Phasellus Placelat,孕妇velit at,pulvinar nulla。Mauris ut faucibus felis,vitae semper Elite。埃尼安·维尔·廷西登·利奥。不要把它当作一种快乐。梅塞纳斯·艾库利斯·波特·托托·布兰迪特。酒酿前酒,酒酿后酒。

谢谢您的回复@Samy Kacimi。不过有一个小问题。。。因此,当我加载页面时,隐藏是隐藏的,但当我单击一个或多个读取时,所有隐藏都会出现。如果你没有注意到我有一个产品循环,如果这有什么不同的话。