我有一個使用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