[英]How to hide all div except the first?
I'm using JQuery and I want to hide all div except the first but the problem is that I display my div (ref_display) by a for loop like that : 我正在使用JQuery,但我想隐藏除第一个之外的所有div,但问题是我通过这样的for循环显示了div(ref_display):
<div class="col-md-6 ref">
{% for benefit in benefits %}
<div class="col-md-6 ref_display">
<h3>{{ benefit.name }}</h3>
<p>{{ benefit.description }}</p>
<div></div>
</div>
{% endfor %}
</div>
And when I use my JS code, it doesn't work : 当我使用我的JS代码时,它不起作用:
$(function() {
var ref = $('div.ref_display');
ref.gt(0).hide();
});
I think JQuery considers that there is only one div when I write .gt(0)
however, I have many of them displayed on my page.. 我认为JQuery认为在编写
.gt(0)
时只有一个div,但是,我的页面上显示了很多。
I'm beginner in JS so there is probably a solution that I don't know :) 我是JS的初学者,所以可能有一个我不知道的解决方案:)
Thank you in advance !! 先感谢您 !!
The issue is that gt(0)
is not a function. 问题在于
gt(0)
不是函数。 Presumably you meant get(0)
but even that has issues as it will return a DOM Element which doesn't have a hide()
method. 大概您的意思是
get(0)
但即使那样也会有问题,因为它将返回一个没有hide()
方法的DOM元素。
To achieve what you need, you can use the :gt
selector to get all divs but the first and hide them: 要实现所需的功能,可以使用
:gt
选择器获取除第一个以外的所有div并将其隐藏:
$('div.ref_display:gt(0)').hide();
That being said you don't need JS/jQuery for this at all as it can be achieved in CSS alone: 话虽如此,您完全不需要JS / jQuery,因为仅在CSS中就可以实现:
div.ref { display: none; } div.ref:first-child { display: block; }
<div class="col-md-6 ref">1</div> <div class="col-md-6 ref">2</div> <div class="col-md-6 ref">3</div>
Try var ref = $('div.ref_display:gt(0)').hide();
尝试
var ref = $('div.ref_display:gt(0)').hide();
$(function() { $('div.ref_display:gt(0)').hide(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-md-6 ref"> <div class="col-md-6 ref_display"> First Div </div> <div class="col-md-6 ref_display"> Second Div </div> <div class="col-md-6 ref_display"> Third Div </div> </div>
I want to hide all div except the first
我想隐藏除第一个以外的所有div
Use :not(:first-child)
使用
:not(:first-child)
$('div.ref_display:not(:first-child)').hide();
Or use slice
或使用
slice
$("'div.ref_display").slice(1).hide();
You can solve it by simply using CSS . 您可以使用CSS来解决它。
:not(:first-child)
will select all the child elements except the first one :not(:first-child)
将选择除第一个元素以外的所有子元素
div:not(:first-child) { display: none;}
<div class="col-md-6 ref_display">1</div> <div class="col-md-6 ref_display">2</div> <div class="col-md-6 ref_display">3</div>
You can use not()
as 您可以使用
not()
作为
var ref = $('div.ref_display').not(':first').hide();
This will ignore the first element with div having class ref_display
. 这将忽略div具有类
ref_display
的第一个元素。
Using only css pseudo selector
not
& first-child
只使用CSS
pseudo selector
not
& first-child
.ref .ref_display:not(:first-child) { display:none; }
<div class="col-md-6 ref"> <div class="col-md-6 ref_display"> <h3>1</h3> <p>1</p> <div>X</div> </div> <div class="col-md-6 ref_display"> <h3>2</h3> <p>2</p> <div>Y</div> </div> <div class="col-md-6 ref_display"> <h3>3</h3> <p>3</p> <div>Z</div> </div> </div>
Seems like you don't JS for this at all. 似乎您根本不使用JS。 Simple CSS should be sufficient
.ref_display + .ref_display
: 简单的CSS应该足够
.ref_display + .ref_display
:
I want to hide all div except the first
我想隐藏除第一个以外的所有div
But jQuery will work too of course: 但是jQuery当然也可以工作:
$('.ref_display + .ref_display').hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-md-6 ref_display">#1</div> <div class="col-md-6 ref_display">#2</div> <div class="col-md-6 ref_display">#3</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.