Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 使用ChereIO在没有子对象的父对象中获取文本_Jquery_Html_Node.js_Cheerio - Fatal编程技术网

Jquery 使用ChereIO在没有子对象的父对象中获取文本

Jquery 使用ChereIO在没有子对象的父对象中获取文本,jquery,html,node.js,cheerio,Jquery,Html,Node.js,Cheerio,我正在尝试使用cheerio提取一个div的内容,而不包含该div的任何子对象。如果我只使用div.text()-我会得到所有的文本-父级和子级。这是HTML-我只想要值“5.25” 下面的代码当前返回“购买价格$5.25” 以下是HTML: <div class="outer tile"> < ... various other html here > <div class="cost"> <span class="

我正在尝试使用cheerio提取一个div的内容,而不包含该div的任何子对象。如果我只使用div.text()-我会得到所有的文本-父级和子级。这是HTML-我只想要值“5.25”

下面的代码当前返回“购买价格$5.25”

以下是HTML:

<div class="outer tile"> 
    < ... various other html here > 
    <div class="cost">
        <span class="text">Purchase price </span>
        <small>$</small>5.25
    </div>
</div>
我用过这个帖子

作为提琴的参考

这对我来说是新的,但是您可以通过只返回nodeType 3的元素来获取文本节点

var a = $('.cost').first().contents().filter(function() {
    return this.nodeType == 3;
});

还有人想知道如何在Cheerio中做到这一点:

$('div.classname').first().contents().filter(function() {
    return this.type === 'text';
}).text();
我最喜欢这个:

$('div.cost').children().remove().end().text();
我觉得更简洁(不知道效率)


请注意,这个答案不同于最高评级的答案,它修改了基础的cheerio对象,并将删除div的内容。您介意解释一下吗?
$('div.cost').children().remove().end().text();