Sunday, 15 August 2010

javascript - querySelectorAll() print textcontent of all nodes -


This is the code that I am using to get all the text content from a webpage. Still it is not working and I do not know what I'm doing.

  & lt; Tr style = "color: # 000000" class = "odd" & gt; & Lt; Td style = "padding: 5px 5px 5px 10px" align = "center" & gt; Input type = "checkbox" name = "card []" id = "card_278002" value = "278002" & gt; & Lt; / TD & gt; & Lt; Td align = "center" & gt; 411756 & lt; / Td> & Lt; Td align = "center" & gt; Sherry & lt; / Td> & Lt; Td align = "center" & gt; 89852 & lt; / Td> & Lt; / TR & gt;  

And this is my JS code:

  function get42 () {return document.query selector all ('tr & gt; td'). TextContent; } Console.log (page.evaluate (get42));  

Output: null .. what am I doing?

You can not use such a document.querySelectorAll . It gives a node list . You have to take textContent from each of your nodes.

Long Way:

  function get42 () {var tds = document.querySelectorAll ('TD'), result = []; (Var i = 0; i & lt; tds.length; i ++) {Results push (tds [i] .textcontent); } Return results; }  

or small:

  function get42 () {var tds = document.querySelectorAll ('td'); Return arrow.protipip.map Cole (TDS, Function (T) {Return T. Content Content;}); }  


No comments:

Post a Comment