有沒有在單個查詢中結合多重選擇器和基本篩選器的方法?將多個選擇器與篩選器結合使用
例如...
var jq = $(someElement);
// Want to find the first following sibling node which has either classA or classB.
// This imaginary jquery does not quite work.
jq.nextAll("(.classA, .classB):first")
// This works. But I wonder if I can achieve the same result with just one query.
jq.nextAll(".classA, classB)").filter(":first")
謝謝你的回答,安迪。如果第一個查詢有多個匹配元素,那麼jQuery會找到所有匹配並選擇第一個?我認爲在單個查詢中這樣做會讓jquery知道我只對第一個元素感興趣,這樣jquery將停止查找任何其他匹配 - 使查詢更快。也許?也許不會? – Kei 2010-10-27 03:20:24
這很有趣。我試圖通過jQuery的源代碼,但不夠好,以瞭解究竟發生了什麼。我發現的第一個函數只是eq函數的別名,它調用this.slice(i,+ i + 1),其中this是結果。所以,我假設無論你如何說出這些類型的複雜選擇器,它都會抓住所有的結果,然後將它們過濾掉。這只是從我可以看到的猜測。我真的不知道它是如何工作的。 – 2010-10-27 03:35:04
@Kei當找到第一個匹配時,無法告訴jQuery停止。請參閱:http://stackoverflow.com/questions/3950888 – 2010-10-27 03:59:38