javascript - 'Show More' & 'Show Less' with jQuery for an article website

标签 javascript jquery html css show-hide

我试图在用户点击“显示更多”时显示额外的一行文章,我想删除带有“显示较少”的那一行。

我有 4 行文章,但我想从 2 行开始,让用户一次添加一行。
我的 jQuery 可能有点乱,因为我从不同的地方收集了点点滴滴。
我通过删除内容简化了代码。

知道为什么它不起作用吗?

<div class="writing-clips">
  <div class="row">
    <h2>Writing Clips</h2>
  </div>
  <div class="row clip-container li">
    <div class="col span-1-of-4 box" id="story-1" href="#">
      <a href="#">
        content
      </a>
    </div>
    <div class="col span-1-of-4 box" id="story-2">
      <a href="#">
        content
      </a>
    </div>           
  </div>
  <div class="row clip-container clips-2 li">
    <div class="col span-1-of-4 box" id="story-5" href="#">
      <a href="#">
        content
      </a>
    </div>
    <div class="col span-1-of-4 box" id="story-6" href="#">
      <a href="#">
        content     
      </a>
    </div>
  </div>
  <div class="row clip-container clips-3 li">
    <div class="col span-1-of-4 box" id="story-9" href="#">
      <a href="#">
        content
      </a>
    </div>
    <div class="col span-1-of-4 box" id="story-10" href="#">
      <a href="#">
        content   
      </a>
    </div>
  </div>
  <div class="row clip-container clips-4 li">
    <div class="col span-1-of-4 box" id="story-13" href="#">
      <a href="#">
        content
      </a>
    </div>
    <div class="col span-1-of-4 box" id="story-14" href="#">
      <a href="#">
        content 
      </a>
    </div>
  </div>
  </section>
<div class="showmore">showmore</div>
<div class="showless">showless</div>

j查询:

$(document).ready(function(){

  size_li = $(".writing-clips .li").size();
  x=2;
  $('.writing-clips .li:lt('+x+')').show();
  $('.showmore').click(function () {
    x= (x+1 <= size_li) ? x+1 : size_li;
    $('.writing-clips .li:lt('+x+')').show();
  });
  $('.showless').click(function () {
    x=(x-1<2) ? 2 : x-1;
    $('.writing-clips .li').not(':lt('+x+')').hide();
  });

});

CSS:

.writing-clips .li { display: none; }

.showmore {
    cursor: pointer;
    color: green;
}

.showmore:hover { color: blue; }

.showless {
    color: red;
    cursor: pointer;
}

.showless:hover { 颜色:蓝色;

最佳答案

在显示前两行之前你需要隐藏所有行

添加这一行

$('.writing-clips .li').hide();

之前

$('.writing-clips .li:lt('+x+')').show();

关于javascript - 'Show More' & 'Show Less' with jQuery for an article website,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44191352/

相关文章:

javascript - CSS 星形单选按钮

jQuery IE7问题(使用jQuery Cycle插件)

javascript - 在事件中播放嵌入式 API Youtube 视频

javascript - 奥斯特里奥 :files & Meteor Remove File

javascript - 调用名称存储在变量中的函数

javascript - Python 和 JavaScript 中的哈希 sha1

javascript - 为什么我可以记录和警报从 jQuery.width() 返回的值,但不能在赋值中使用?

html - 如何在 Firefox 上使用 css 使伪元素可见和可点击

javascript - 无法使用 Node.js 通过 HTML 访问 JavaScript 静态文件

javascript - 调整 Django Rest 框架模板