使用cheerio选择div标签下的所有文本

问题描述 投票:-1回答:1
<div id="ftext">
    A
    <br><br>
    B
    <br><br>
    C
    <br><br>
    <div style="float:right;">
     D
    </div>
    <div class='clear'></div>
</div>

我想选择#ftext下的“A B C”并将其作为字符串存储到变量中,并将“D”存储在另一个变量中。使用cheerio。谢谢!

javascript html node.js cheerio
1个回答
0
投票

B和C是单独的节点,因此可以加入它们:

let abc = $("#ftext")[0].children.map(e => e.nodeValue).join('')

对于D,可以选择具有样式属性的div,如下所示:

let d = $("#ftext div[style]").text()
© www.soinside.com 2019 - 2024. All rights reserved.