【问题标题】:Adding show more show less button添加显示更多显示更少按钮
【发布时间】:2018-05-17 21:26:06
【问题描述】:

在我的网站中,为了避免复杂性,我尝试使用 jQuery 添加显示越来越多的选项。但它不起作用。

我在这里尝试显示前 10 个列表。

点击显示更多后,应显示现有列表

//this will execute on page load(to be more specific when document ready event occurs)
if ($('.ty-compact-list').length > 10) {
  $('.ty-compact-list:gt(2)').hide();
  $('.show-more').show();
}

$('.show-more').on('click', function() {
  //toggle elements with class .ty-compact-list that their index is bigger than 2
  $('.ty-compact-list:gt(2)').toggle();
  //change text of show more element just for demonstration purposes to this demo
  $(this).text() === 'Show more' ? $(this).text('Show less') : $(this).text('Show more');
});
.ty-compact-list {
  float: left;
}

.show-more {
  display: none;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container menuitem wrapper">
  <div class="filterDiv ty-compact-list soup"><img class="img1" src="images/a1.jpg">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list indo"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list soup"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list devil"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list platter"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list devil"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list starter"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list starter"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list fry"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list d&i"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list fry"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list fry"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>
  <div class="show-more">Show more</div>
</div>

【问题讨论】:

  • 创建一个 sn-p 并包含 jQuery 似乎可以使其按预期工作?也许您需要将代码包装在 $(function() { .... }); 中?
  • 你想要function open() { $('aaa').one('click', close)};function close({ $('aaa').one('click', open)};$('aaa').one('click', open);
  • 我找不到问题出在哪里..

标签: javascript css


【解决方案1】:

您的代码有一些问题。

1-$('.show-more').css("display", "block");

2-$(this).text($(this).text() === '显示更多' ? '显示更少' : '显示更多');

3- 从 css 中删除 display: none;

if ($('.ty-compact-list').length > 3) {
  $('.ty-compact-list:gt(2)').hide();
  $('.show-more').css("display", "block");
}

$('.show-more').on('click', function() {
  //toggle elements with class .ty-compact-list that their index is bigger than 2
  $('.ty-compact-list:gt(2)').toggle();
  //change text of show more element just for demonstration purposes to this demo
  $(this).text($(this).text() === 'Show more' ? 'Show less' : 'Show more');
});
.ty-compact-list {
  float: left;
}

.show-more {
  cursor: pointer;
}

.filterDiv {
  float: left;
  background-color: #ffffff;
  width: 450px;
  text-align: center;
  margin: 6px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  border-radius: 20px;
  border: 2.5px solid #0f2f16;
  min-height: 300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container menuitem wrapper">
  <div class="filterDiv ty-compact-list soup"><img class="img1" src="images/a1.jpg">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list indo"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list soup"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list devil"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list platter"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list devil"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list starter"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list starter"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list fry"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list d&i"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list fry"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>

  <div class="filterDiv ty-compact-list fry"><img class="img1" src="images/a1.jpg" style="float: left">
    <h2 class="head2">onion soup</h2>
    <h3 class="head3">$200</h3>
    <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div>
  <div class="show-more">Show more</div>
</div>

【讨论】:

  • 先生。谢谢你..filterDiv {浮动:左;背景颜色:#ffffff;颜色:#ffffff;宽度:450px;文本对齐:居中;边距:6px;盒子阴影:0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);边框半径:20px;边框:2.5px 实心#0f2f16; }
  • min-height: 300px; 添加到您的 css 部分,以便所有项目都相等。 @ArebhySri
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-01
  • 1970-01-01
  • 2020-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-28
相关资源
最近更新 更多