获得第n个子也各行中的组合选择相匹配

问题描述:

说我有下面的标记获得第n个子也各行中的组合选择相匹配

<div><p></p><blockquote></blockquote><ul>...</ul></div> 
<div><p></p><blockquote></blockquote><ul>...</ul></div> 
... 
<div><p></p><blockquote></blockquote><ul>...</ul><ul></ul></div> 

是否有一个jQuery选择/穿越技术,我可以用它来获得第n个子匹配复杂在每个格选择具有鲁棒性(在我可以改变不选择匹配的元素不搞乱事情的意义上)

这里有一些方法,不工作,帮助说明问题

$("div").children("p, ul").filter(":eq(1)") // returns just the first <ul> in the first div 
$("div").children("p, ul:eq(1)") // returns all the p's and the first ul in the first div 
$("div").find(":not(blockquote):eq(1)") //returns the correct elements, but at the expense of having to reference the elements we're *not* after. Also not restricted to just children 
$("div").find(">:not(blockquote):eq(1)")) // tackles the children problem, but ">" without a parent is not officially supported and jQuery want to deprecate (Can't find the reference but I raised it once in discussion on the jQuery bug boards and John Resig himself said this) 

最后,对于一些背景,我为了总能找到一个表的第i列问这个不管第一列是否包含thtd细胞

想过后,我意识到需要的是某种:is()选择器来包装化合物选择器。 jQuery将无法实现它(for good reasons),但后来我意识到一个双重否定是同为正所以一般情况下

$("div").find(">:not(:not(p, ul)):eq(1)") 

是jQuery有一个nth-child selector

jQuery - :nth-child() Selector

+0

以下工作不能相信我错过了。干杯 – wheresrhys 2012-02-07 15:45:45

+0

;)没问题,乐于帮忙! – dknaack 2012-02-07 15:46:10

+0

啊......但考虑到它仍然不适用于一般情况(虽然完美地解决了我的表列问题:)),因为我们不寻找父母的第n个孩子,我们正在寻找对于与选择器匹配的第n个子元素来说,这是不完全相同的。我正在制作一个jsfiddle来演示,但是该网站是越野车,但基本上,如果前几个元素与选择器不匹配,那么nth-child不会指向第n个匹配的子节点,它仍然会指向第n个孩子(我想......这就是我想的jsfiddle来测试) – wheresrhys 2012-02-07 15:59:45