jquery 显示/隐藏 div 和计数器

发布于 2024-09-28 07:31:37 字数 966 浏览 2 评论 0原文

我有随机数量的 DIV(最少 1 个,最多 10 个),

<div id="container">
<div class="foo">Content</div> <!-- div 1 -->
<div class="foo">Content</div> <!-- div 2 -->
<div class="foo">Content</div> <!-- div 3 -->
<div class="foo">Content</div> <!-- div 4 -->
<div class="foo">Content</div> <!-- div 5 -->
<div class="foo">Content</div> <!-- i need this one hidden -->
<div class="foo">Content</div> <!-- and this one -->
</div>

我希望前 5 个 div 可见(使用 .show() 或使用类,都没关系)。任何额外的 DIV 都应该隐藏。

然后,我使用以下命令模拟 div 的“关闭”:

$('.foo').click(function(){
    $(this).fadeOut('slow');    
});

它将删除单击的 div,导致下面的所有 div 上移一位。这就是我想要的效果。

但是,我在这里需要一些逻辑。

如果我的 DIVS 少于 5 个,则应禁用关闭设施。如果我有超过 5 个 DIV,那么当一个 div “关闭”时,我希望下一个“隐藏”div 变得可见。

如果需要,我可以向每个 DIV 添加 ID,例如“foo1”、“foo2”等 ID。

I have a random amount of DIVs (minimum 1, max of 10)

<div id="container">
<div class="foo">Content</div> <!-- div 1 -->
<div class="foo">Content</div> <!-- div 2 -->
<div class="foo">Content</div> <!-- div 3 -->
<div class="foo">Content</div> <!-- div 4 -->
<div class="foo">Content</div> <!-- div 5 -->
<div class="foo">Content</div> <!-- i need this one hidden -->
<div class="foo">Content</div> <!-- and this one -->
</div>

I want the first 5 divs to be visible (either with .show() or with a class, doesn't matter). Any additional DIVs should be hidden.

I then simulate the "closing" of a div with:

$('.foo').click(function(){
    $(this).fadeOut('slow');    
});

which removes the clicked div, causing all the divs below to move up one. This is my desired effect.

However, I need some logic here.

If I have less than 5 DIVS, the close facility should be disabled. If I have more than 5 DIVs, then when a div is "closed", i want the next "hidden" div to become visible.

I can add IDs to each DIV if required with IDs like "foo1" "foo2" etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拿命拼未来 2024-10-05 07:31:37

像这样的东西应该可以工作:

$("#container .foo:gt(4)").hide();

$("#container").delegate(".foo", "click", function() {
    if(!$("#container .foo:hidden").length) return;
    $(this).fadeOut('slow', function() { 
        $(this).siblings(":hidden:first").fadeIn()
               .end().remove();
    });
});

您可以在这里测试。其作用是使用 :gt(4)(从 0 开始)选择器。然后我们使用 .delegate() 来提高效率(尽管 < a href="http://api.jquery.com/click/" rel="nofollow">.click() 也可以)。如果没有更多的隐藏,就没有效果。如果有更多隐藏的内容,请淡出我们单击的内容,在淡入淡出的末尾显示 :first :hiddden< /code>一,以及 .remove()我们完全消失了。

Something like this should work:

$("#container .foo:gt(4)").hide();

$("#container").delegate(".foo", "click", function() {
    if(!$("#container .foo:hidden").length) return;
    $(this).fadeOut('slow', function() { 
        $(this).siblings(":hidden:first").fadeIn()
               .end().remove();
    });
});

You can test it out here. What this does is hides all past 5 using the :gt(4) (0-based) selector. Then we're using .delegate() for efficiency (though a .click() would work too). If there aren't any more hidden, there's no effect. If there are more hidden, fade out the one we clicked, at at the end of the fade show the :first :hiddden one, and .remove() the one we faded out completely.

友欢 2024-10-05 07:31:37
$('.foo').each(
   function(index,element) {
       if(index>5) $(element).hide();
   }
)
$('.foo').each(
   function(index,element) {
       if(index>5) $(element).hide();
   }
)
原谅过去的我 2024-10-05 07:31:37
$('#container div.foo').click(function() {
  if ($(this).index() >= 5) ...; //etc
}
$('#container div.foo').click(function() {
  if ($(this).index() >= 5) ...; //etc
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文