[英]Is there simple way to get array from td elements on page? (node.js/cheerio/jQuery)
我正在使用 node.js/cheerio(與 jQuery 語法相同)來解析頁面。 當我使用時:
$ = cheerio.load(body);
console.log($('.td-title a').text());
我的控制台中有很長的字符串,來自單詞“mainaboutcontactusprofile”等。如何制作一組 td 文本?
提供的答案都不適合我,但確實如此。
let arr = $('.td-title a').toArray().map((x) => { return $(x).text()});
信用: Github
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>
我建議為此使用 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 );
有關更多詳細信息,請參閱http://api.jquery.com/map/
在 javascript 中只需 document.getElementsByTagName("td")返回所有 td的數組。
使用 jQuery 你應該使用類似 $('#table1 td')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.