2017-06-29 8 views
1
let playersCell = ` 
    <td class="foo" colspan="2"> 
     <a href="example.com"> 
     <span class="bold">John Beluga</span> 
     - Sarah Jay. 
     </a> 
    </td> 
    ` 

let players = cheerio.load(playersCell) 
players.find('a').html() 

私はcheerio.jsにHTML文字列をロードし、aタグを見つけようと、私はplayers.findはチェリオオブジェクトの機能ではありません

ため

[TypeError: players.find is not a function]

Console.logショーを取得しています

enter image description here

+0

あなたはこの '聞かせて選手= cheerio.load(playersCell)を試すことができます。 players( '。foo')。find( 'a').html() ' –

答えて

1

findは、DOM検索結果に表示される方法です。 findを使用する前に結果を作成する必要があります。例えば

let playersCell = `<table><tr> 
 
    <td class="foo" colspan="2"> 
 
     <a href="example.com"> 
 
     <span class="bold">John Beluga</span> 
 
     - Sarah Jay. 
 
     </a> 
 
    </td></tr></table> 
 
    ` 
 

 
let players = cheerio.load(playersCell); 
 
console.log(players('td').find('a').html());
<script src="https://wzrd.in/standalone/[email protected]"></script>

しかし、この場合には、する必要はありません。あなただけの直接初期検索を使用することができます。

0

let playersCell = ` 
 
    <td class="foo" colspan="2"> 
 
     <a href="example.com"> 
 
     <span class="bold">John Beluga</span> 
 
     - Sarah Jay. 
 
     </a> 
 
    </td> 
 
    ` 
 

 
let players = cheerio.load(playersCell); 
 
console.log(players('a').html());
<script src="https://wzrd.in/standalone/[email protected]"></script>
私は .find is not a functionを得た、と私は私が見つけることを試みていたコンソールでオブジェクトを見たとき、それはタイプが tagだったと述べ。私は再びオブジェクトをラップする必要があることを知った。

let results = $('.your .query') 
results.each((i, r) => { 
    $(r).find('.your .next .query') 
}) 
関連する問題