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

时间:2013-09-15 09:08:28

标签: javascript jquery css

我有点卡在这里。我正在尝试使用jQuery显示每个部分的第一个div。有多个父元素.each-business-content-extra,其中包含一些子元素.each。每个.each设置为通过CMS显示无,但我希望每个.each中存在的第一个.each-business-content-extra设置为显示:阻止。

我试过了 -

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

有什么想法吗?

谢谢, [R

6 个答案:

答案 0 :(得分:2)

您可以使用.eq()来定位每个.each类的第一个元素。 http://api.jquery.com/eq/

试试这个:

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

或者,试试这个:

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

答案 1 :(得分:1)

试试这个:

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

答案 2 :(得分:1)

您可以按照自己的方式进行操作,但正确的选择器为:first-of-type

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

答案 3 :(得分:1)

你可以试试这个简单的,

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

答案 4 :(得分:1)

尝试

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

演示:Fiddle

答案 5 :(得分:1)

原来这是每个人的组合......

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

非常感谢。