【问题标题】:show hide no of divs according to the current index jquery根据当前索引jquery显示隐藏div的数量
【发布时间】:2023-03-27 23:35:01
【问题描述】:

我正在使用 jQuery 索引。这里我需要根据当前索引添加和删除div。

我要寻找的是,当我的当前索引大于 7 时,我需要删除前四个 div,并且当我的当前索引小于四 (4) 时,我需要再次显示那些删除的前四个 div。

我使用 :lt(4) 隐藏前四个 div。但我不知道如何让它重新显示。

提前致谢

$(window).load(function() {

  $(document).keydown(function(e) {
	  if (e.keyCode == 37){

		 


	  }
	  else if (e.keyCode == 39){

		 


	  }
	  else if (e.keyCode == 40){

		  var cIndex = $('.foo.active').index();
      
          if(cIndex > 7) {

              $('.test').find('.foo:lt(4)').remove();

          }


	  }
	   else if (e.keyCode == 38){

		  var cIndex = $('.foo.active').index();
      
          if(cIndex < 4) {

              $('.test').find('.foo:lt(4)').add();

          }


	  }
  });
  
});  
.test {
  width: 420px;
  height: 200px;
  text-align: center;
}

.foo {
  width: 100px;
  height: 100px;
  line-height: 100px;
  display: inline-block;
  background: #ccc;
  margin-bottom: 4px;
}

.foo.active {
  background: #565656;
  color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test">
    <div class="foo active">1</div>
    <div class="foo">2</div>
    <div class="foo">3</div>
    <div class="foo">4</div>
    <div class="foo">5</div>
    <div class="foo">6</div>
    <div class="foo">7</div>
    <div class="foo">8</div>
    <div class="foo">9</div>
    <div class="foo">10</div>
    <div class="foo">11</div>
    <div class="foo">12</div>
</div>

【问题讨论】:

  • 粘贴你的javascript代码.. :)
  • @jai, @ void..我更新了我的代码

标签: javascript jquery html key-events


【解决方案1】:

您可以将hide 类添加到您要隐藏的元素中,然后当您想要显示它们时使用该类并选择它们,就像这样:

$('.foo:lt(4)').addClass('hide').fadeOut();
// when you show them back
$('.foo.hide').removeClass('hide').fadeIn();

【讨论】:

  • 这更像是一个评论而不是一个答案
猜你喜欢
  • 2015-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-21
  • 2012-08-06
  • 1970-01-01
相关资源
最近更新 更多