从给定元素的第一个兄弟获取文本

时间:2016-07-02 07:02:41

标签: jquery

这个jQuery喜欢选择器" cheerio"试图获取文字"牛奶"来自HTML。但失败了。 ' $'是cheerio对象。
 怎么做到呢?感谢

<!-- food -->
<tr>
  <td class="label" valign="top" align="right" style="white-space:nowrap">
    drinking:&nbsp;&nbsp;
  </td>
  <td class="displayValue" valign="top">
    milk
  </td>
</tr>
$('*').contents().filter(function () {
  if (this.nodeType == 8) {           //found the comments node / title

    // this produced both the texts 'drinking' and 'milk'
    console.log($(this).next().text());  

  }
});

1 个答案:

答案 0 :(得分:0)

此代码可能对您有所帮助。:

$('*').contents().filter(function () {
  if (this.nodeType == 8) {           //found the comments node / title
    console.log($(this.nextSibling).text());  //did work
  }
});

出于某些原因,对我来说不明显,$(this).next();无法正常工作。所以,我使用了原生的JS nextSibling;

相关问题