如何在Cheerio中迭代第一个表的行



假设我们有两个表的简化版本:

<table class="test">
<tr>some text</tr>
<tr>another text</tr>
</table>
<table class="test">
<tr>it's a text</tr>
<tr>different text></tr>
</table>

如何用class="test"迭代第一个<table><tr>元素?

你可以做:

$('table.test').first().find('tr').each(function(i, tr){
console.log($(tr).text())
})

如果它们真的紧挨着,你可以这样做:

$('table.test:nth-of-type(1) tr').each(function(i, tr){
console.log($(tr).text())
})