获得每tr的td值

use*_*406 5 javascript jquery

我有以下风格的代码:

<tr id="201461">
      <td id="0A" style="cursor:pointer" onClick = "ProcessTextBoxClick()" value="Feb 23 2008">Feb 23 2008</td>
      <td id="0B" style="cursor:pointer" onClick = "ProcessTextBoxClick()" value="Feb 25 2008">Feb 25 2008</td>
      <td id="0C" style="cursor:pointer" onClick = "ProcessTextBoxClick()" value="Feb 28 2008">Feb 28 2008</td></tr><tr id="201460">
       <td id="1A" style="cursor:pointer" onClick = "ProcessTextBoxClick()" value="47">47</td></tr>
Run Code Online (Sandbox Code Playgroud)

我有一些JQuery,我获取每行的id,现在我想获得每行的每个td中的每个值.我该怎么做呢?

 var tbl = document.getElementById("tbl-1");

    var numRows = tbl.rows.length;

    for (var i = 1; i < numRows; i++) {

        var ID = tbl.rows[i].id;
Run Code Online (Sandbox Code Playgroud)

Aro*_*eel 17

你的代码看起来不像jQuery.你确定你没有使用jQuery一词作为JavaScript的同义词吗?:)如果是这种情况,我建议你也阅读这个问题 ; 它会让事情变得更加清晰.

无论如何,这里是JavaScript:

var tbl = document.getElementById("tbl-1");
var numRows = tbl.rows.length;

for (var i = 1; i < numRows; i++) {
    var ID = tbl.rows[i].id;
    var cells = tbl.rows[i].getElementsByTagName('td');
    for (var ic=0,it=cells.length;ic<it;ic++) {
        // alert the table cell contents
        // you probably do not want to do this, but let's just make
        // it SUPER-obvious  that it works :)
        alert(cells[ic].innerHTML);
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,如果你真的使用jQuery:

var table = $('#tbl-1').
var rowIds = [];
var cells = [];
$('tr', table).each(function() {
    rowIds.push($(this).attr('id'));
    $('td', $(this)).each(function() {
        cells.push($(this).html());
    });
});
// you now have all row ids stores in the array 'rowIds'
// and have all the cell contents stored in 'cells'
Run Code Online (Sandbox Code Playgroud)


KAR*_*ván 5

在jQuery中:

$("table#tbl-1 tr").each(function( i ) {
  $("td", this).each(function( j ) {
    console.log("".concat("row: ", i, ", col: ", j, ", value: ", $(this).text()));
  });
});
Run Code Online (Sandbox Code Playgroud)

你可以在这里查看它:http://jsfiddle.net/3kWNh/