最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there simple way to get array from td elements on page? (node.jscheeriojQuery) - Stack Overflow

programmeradmin2浏览0评论

I am using node.js/cheerio (the same as jQuery syntax) to parse the page. And when I am using:

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

I have in my console very long string from words "mainaboutcontactusprofile" etc. How can I make an array of td text?

I am using node.js/cheerio (the same as jQuery syntax) to parse the page. And when I am using:

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

I have in my console very long string from words "mainaboutcontactusprofile" etc. How can I make an array of td text?

Share Improve this question edited Sep 19, 2017 at 9:27 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Sep 18, 2017 at 23:27 Angry BAngry B 3052 silver badges18 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 7

None of the answers provided worked for me, but this did.

let arr = $('.td-title a').toArray().map((x) => { return $(x).text()});

Credit: Github

jQuery's map()

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

I suggest using jQuery's $.map() function for this. It creates an array based on a matched set of elements:

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

See http://api.jquery./map/ for more details

in javascript simply document.getElementsByTagName("td") return an array of all td.

Using jQuery you should use something like $('#table1 td')

发布评论

评论列表(0)

  1. 暂无评论