【问题标题】:How do I get the value of the $ (this) .children inner element attribute?如何获取 $ (this) .children 内部元素属性的值?
【发布时间】:2021-06-13 06:54:04
【问题描述】:

有这样一个库代码Cheerio

let outputTableBody = $('.box.table-quotes table.table tr').each(function(i, elem) {
   let expression = $(this).children('td:nth-child(1)').text();


   if(expression == companyNameArray[ExchangeNameParse]){
     $(this).children('td').each(function(i, elem) {
       if( i == 1 ){
         // do something
         }
       })
     }
})

在这个例子中,我可以提取td 元素的文本值——不管那里是否有额外的父元素——文本仍然会被正确提取。

问题:
如果这样的标签 (td) 包含子标签 a - 在此构造中如何提取其href 属性的值?

【问题讨论】:

  • $(this).children('a').attr('href')
  • 这不起作用:joxi.ru/D2PgJPYhJOOZym
  • 打印$(this).children('a')
  • $(this).find('a').attr('href')
  • @hoangdv 请将此添加为答案。 OP 确保你接受他的回答

标签: javascript jquery node.js cheerio


【解决方案1】:

您可以使用.find 方法获取当前匹配元素集中每个元素的后代,由选择器过滤 - 在本例中为a

...
const link = $(this).find('a').attr('href');
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 2021-08-07
    相关资源
    最近更新 更多