简体   繁体   中英

Selecting td element in a tr

Here is the code I want to process using javascript / jquery

EDITED THE BELOW CODE BLOCK FOLLOWING THE COMMENTS

    <table>
       <tbody>
<tr class="promote">
           <td>1</td>
           <td><span class="team_logo visible-lg visible-sm" style=""></span><a class="team_name " href="/team/show/7473/Power-Hiters">Power Hiters</a><span class="online" title="user is online"></span></td>
           <td style="text-align:center">5</td>
           <td style="text-align:center">10</td>
           <td style="text-align:center">5</td>
           <td style="text-align:center">0</td>
           <td style="text-align:center">0</td>
           <td style="text-align:center">6.713</td>
        </tr>
<tr> <!-- Similar Above tr Content --> </tr>
          <tr> <!-- Similar Above tr Content --> </tr>
          <tr> <!-- Similar Above tr Content --> </tr>
       </tbody>
    </table>

I want to get the inner content/html of each <td> tag in a array using javascript / jquery


Extra Info:

When I tried it seems jquery strips the td and tr tags.

Actual thing I want to do is extract td in a multi dimensional array for each tr the table structure:

<table>
   <tbody>
      <tr> <!-- Above tr Content --> </tr>
      <tr> <!-- Above tr Content --> </tr>
      <tr> <!-- Above tr Content --> </tr>
   </tbody>
</table>

Since you want a multidimensional array, you will need to:

  • Make an array that will hold all table data
  • Iterate through all tr and create an array per row
  • Push that array in the first one, that holds all table data.

something like this:

var tableContent = [];
$('table tr').each(function(){
  var trcontent = [];
  $('td', this).each(function(){ trcontent.push($(this).text()); });
  tableContent.push(trcontent);
})

console.log(tableContent);

example in jsfiddle

尝试,

var arr = $("#cache").find('td').map(function(){ return this.textContent; }).get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM