2017-09-18 2 views
0

私はnode.js/cheerio(jQuery構文と同じ)を使ってページを解析しています。 そして、私が使用しています:ページ上のtd要素から配列を取得する簡単な方法はありますか? (node.js/cheerio/jQuery)

$ = cheerio.load(body); 
console.log($('.td-title a').text()); 

私は私のコンソールで「mainaboutcontactusprofile」の言葉から、非常に長い文字列を持っているなどがどのように私は、TDテキストの配列を作ることができますか?

答えて

0

これはjQueryの$ .map()関数を使用することをお勧めします。

var textArray = $('.td-title a').map(function(index, domElement) { 
    return domElement.innerText; //each element input gets turned into text output 
}); 
console.log(textArray); 

が 単にdocument.getElementsByTagName(詳細はJavaScriptで

1

jQuery's map()

console.log($("td").map(function(){ return this.textContent}).get());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <tr> 
 
    <td>a</td> 
 
    <td>b</td> 
 
    <td>c</td> 
 
    </tr> 
 
</table>

0

ためhttp://api.jquery.com/map/を参照:これは、要素の一致セットに基づいて配列を作成します"td")返品配列すべてのtd。

jQueryを使用すると、$( '#table1 td')のようなものを使用する必要があります

関連する問題