【问题标题】:nodejs cheerio selecting child element inside an each loopnodejs Cheerio 在每个循环中选择子元素
【发布时间】:2018-08-03 02:54:24
【问题描述】:

我正在尝试选择 .info 类中的 strong a 标签

$(".info").each(function(i, item){
    console.log($(this).children("strong a").text())
});

它正确选择了信息类,而不是strong a

【问题讨论】:

  • 有很多 .info's 所以这行不通我不认为,我必须循环 .info strong a @31piy

标签: javascript jquery node.js cheerio


【解决方案1】:

你应该可以的

$(".info").each(function(i, item){
    console.log($("strong a", item).text())
});

【讨论】:

  • @SkylarLopez children 仅考虑该节点的第一级子节点。对于您所做的事情,您应该使用find,但我发现这个速记更简洁
【解决方案2】:

在循环内,也可以这样使用

$(".info").each(function(i, item){
   $(this).find("strong a").text();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    相关资源
    最近更新 更多