用cherrio提取DOM元素

时间:2016-08-01 23:03:45

标签: jquery

此Meteor服务器代码无法提取文本" 789"使用cherrio对象$作为docs建议。

$('td[headers=x]').each(function() {
  console.log($(this).next().html().trim());                  //=> 456
  console.log($(this).next('td[headers="y"]').html().trim()); //<-- fail
});
<td headers="x" class="bodyTextSmall">
  123
</td>
<td headers="xx" class="bodyTextSmall">
  456
</td>
<td headers="y" class="bodyTextSmall">
  789
</td>

知道为什么吗?感谢

1 个答案:

答案 0 :(得分:0)

.next()只返回 immediate 下一个兄弟 - 如果使用了一个选择器,那么它必须匹配。你的headers="yy"不是下一个兄弟,而是下一个。改为使用.siblings()与选择器:

$(this).siblings('td[headers="y"]').first().html().trim()