使用JQuery选择类似的DOM元素

时间:2015-10-27 10:59:59

标签: javascript jquery google-chrome-extension

我正在编写一个chrome插件,我正在使用ycombinator news

进行测试

我能够获得新闻链接的CSS选择器。以下是前三个链接的css选择器

html > body > center > table > tbody > tr:nth-child(3) > td > table > tbody > tr:nth-child(1) > td:nth-child(3) > a
html > body > center > table > tbody > tr:nth-child(3) > td > table > tbody > tr:nth-child(4) > td:nth-child(3) > a
html > body > center > table > tbody > tr:nth-child(3) > td > table > tbody > tr:nth-child(7) > td:nth-child(3) > a

现在我想使用前两个或三个css选择器选择所有其他类似的链接。它可以是任何其他元素,也不仅仅是链接。 在jquery中是否有任何方法可以实现这一点。

3 个答案:

答案 0 :(得分:0)

你可以使用多重选择器操作符(","):https://api.jquery.com/multiple-selector/

$("html > body > center > table > tbody > tr:nth-child(3) > td > table > tbody > tr:nth-child(1) > td:nth-child(3) > a, 
html > body > center > table > tbody > tr:nth-child(3) > td > table > tbody > tr:nth-child(4) > td:nth-child(3) > a, 
html > body > center > table > tbody > tr:nth-child(3) > td > table > tbody > tr:nth-child(7) > td:nth-child(3) > a")

或者,更恰当地说:

$("html > body > center > table > tbody > tr:nth-child(3) > td > table > tbody").find("tr:nth-child(1), tr:nth-child(4), tr:nth-child(7)").find("td:nth-child(3) > a")

答案 1 :(得分:0)

您可以使用~

html > body > ... > td:nth-child(3) > a ~ p

这将选择a的父级内的所有段落,或者我会说兄弟元素p

答案 2 :(得分:0)

据我所知,你想要CSS规则:

html > body > center > table > tbody > tr:nth-child(3) > td > table > tbody > tr:nth-child(3n+1) > td:nth-child(3) > *

参见:tr:nth-child(3n+1)针对1,4,7,10等... & > *获取所有类型的孩子,而不仅仅是锚

相关问题