简体   繁体   English

显示父元素列表中的每个第一个元素(jQuery + CSS)

[英]Show each first element in a list of parent elements (jQuery+CSS)

I'm a bit stuck here. 我有点卡在这里。 I'm trying to show the first div for each section I have using jQuery. 我试图显示我使用jQuery的每个部分的第一个div。 There's multiple parent elements .each-business-content-extra which has a few child elements .each . 有多个父元素.each-business-content-extra ,其中有一些子元素.each Each .each is set to display none via the CMS, but I want the first .each in each .each-business-content-extra that exists to be set to display:block. 每个.each均设置为通过CMS不显示任何内容,但我希望将每个.each-business-content-extra中的第一个.each设置为display:block。

I tried this — 我试过了

$('.each-business-content-extra .each:first').each(function() {
  $(this).show();
});

Any ideas? 有任何想法吗?

Thanks, R 谢谢,R

You can use .eq() to target the first element of each .each class. 您可以使用.eq()定位每个.each类的第一个元素。 http://api.jquery.com/eq/ http://api.jquery.com/eq/

Try this: 尝试这个:

$('.each-business-content-extra .each:eq(0)').each(function() {
  $(this).show();
});

Alternatively, try this: 或者,尝试以下操作:

$('.each-business-content-extra .each:eq(0)').css('display', 'block');

尝试这个:

  $('.each-business-content-extra').children(":first").show();

You can do it your way, but the correct selector is :first-of-type : 您可以按照自己的方式做,但是正确的选择器是:first-of-type

$('.each-business-content-extra .each:first-of-type').each(function() {
  $(this).show();
});

您可以尝试这个简单的方法,

$('.each-business-content-extra .each:first-child').show();

Try 尝试

$('.each-business-content-extra').find('.each:first').show()

Demo: Fiddle 演示: 小提琴

Turns out it was a combination of everyones... 原来这是每个人的结合...

$('.each-business-content-extra').each(function() {
  $(this).find('.each:first').show();
});

Thanks so much. 非常感谢。

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

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