简体   繁体   English

在第5个div之后隐藏所有其他div

[英]Hide all other divs after the 5th div

I am trying to hide the divs after photo info 5. Not sure if the script below is executing it correctly or not. 我正在尝试在照片信息5之后隐藏div。不知道下面的脚本是否正确执行了它。 If anyone could give me any guidance that would be great. 如果有人可以给我任何指导,那就太好了。

jQuery jQuery的

$( ":nth-of-type(5)" ).nextAll( ".photo-info" ).addClass( "hidden" );

HTML HTML

<div class="photo-info">1</div>
<div class="photo-info">2</div>
<div class="photo-info">3</div>
<div class="photo-info">4</div>
<div class="photo-info">5</div>
<div class="photo-info">6</div>
<div class="photo-info">7</div>

Your script is working based on the code you provided. 您的脚本正在根据您提供的代码工作。

Given that they are all sibling elements, you could do this with pure CSS by making use of the general sibling combinator, ~ : 假设它们都是同级元素,则可以通过使用通用的同级组合器~ :使用纯CSS 完成此操作

Example Here 这里的例子

.photo-info:nth-of-type(5) ~ .photo-info {
    display: none;
}

Alternatively, you could also use .eq(4) ( 0 index based): 另外,您也可以使用.eq(4) (基于0索引):

Updated Example 更新示例

$('.photo-info').eq(4).nextAll('.photo-info').addClass('hidden');

You could also use :gt(4) : 您也可以使用:gt(4)

Updated Example 更新示例

$('.photo-info:gt(4)').addClass('hidden');

For what it's worth, you may need to wrap your jQuery in a DOM ready handler: 对于它的价值,您可能需要将jQuery包装在DOM ready处理程序中:

$(document).ready(function () {
    $('.photo-info:gt(4)').addClass('hidden');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM