我有一个使用jQuery选项卡的页面。每个选项卡都包含一个或多个jQuery手风琴,它们是动态生成的,除了其他内容。例如:jQuery在每个选项卡中选择第一个手风琴
<div id="tab1" class="tab">
<div>
Some stuff
</div>
<div class="accordion">
I am an accordion
</div>
</div>
<div id="tab2" class="tab">
<div class="accordion">
I am also an accordion
</div>
More stuff
<div class="accordion">
I am also an accordion
</div>
</div>
我希望每个选项卡中的第一个手风琴保持打开状态,而其他(如果有的话)折叠。我曾尝试过:
$('.tab .accordion:first')
其中只显示页面上的第一个手风琴(显然)。我也试过:
$('.tab .accordion:first-child')
这将选择在TAB2第一手风琴,但它没有选择,因为有上面的一些东西之一,TAB1。我也试过:
$('.tab > .accordion').filter(':first-child')
$('.tab').children('.accordion:first-child')
除了我能想到的选择器的每个组合。此时我的大脑被炸了。你点我到重复的问题之前,这些都不是问同样的问题正是:
JQuery Tab each Selected tab first text box focus
jquery select first child with class of a parent with class
jQuery selector for each first element on every parent
jQuery Selecting the first child with a specific attribute
在我的情况不同的是我几乎不能控制这些标签中显示的内容。
如何$( '标签 ')每个(函数(){VAR firstAcc = $(本).find(' 手风琴。')EQ(0); })? – gaurav 2013-02-16 00:15:51
废话...我一问就找到了答案。 '$('。accordion:first-of-type')'这是免费赠品。 – 2013-02-16 00:19:47
实际上,':first-of-type' _doesn't_不在我上面提供的例子中工作,因为它匹配第一个div元素,而不是第一个带有手风琴类的元素。请参阅下面的David Thomas的回答。 – 2013-02-16 00:58:36