我可以像这样获得选定的单元格textContent:
this.textContent
但是我想在选定的单元格之前获取单元格的textContent。我怎样才能做到这一点?。我这样尝试过,但它说undefined
:
$('#dtBasicExample').on('click', 'tbody td', function() {
var table = $('#dtBasicExample').DataTable();
var colIndex = table.cell(this).index().column-1;//get previous column index
var rowIndex = table.cell(this).index().row;//get row index
var Text=table.cells(rowIndex, colIndex).textContent;//get textContent(not working)
alert(Text);
})
答案 0 :(得分:1)
使用table.cell(rowIndex, colIndex).data()
代替table.cells(rowIndex, colIndex).textContent;
var table = $('#dtBasicExample').DataTable();
$('#dtBasicExample').on('click', 'tbody td', function () {
var colIndex = table.cell(this).index().column - 1; //get previous column index
var rowIndex = table.cell(this).index().row; //get row index
// Use data() instead of textContent and cell instead of cells
var text = table.cell(rowIndex, colIndex).data();
alert(text);
});
答案 1 :(得分:1)
已触发元素的Jquery对象内的数据表检查_DT_CellIndex
属性可确定列和行。
我们可以在将_DT_CellIndex加载到Datatable之前简单地进行修改
示例代码:
$('#example').on('click', 'td', function() {
var table = $(this).closest('table').DataTable();
$(this)[0]._DT_CellIndex.column-=1;
alert(table.cell($(this)).data());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet" />
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Numero</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>155555</td>
<td>2011/04/25</td>
<td>$320,800</td>
</tr>
<tr>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>$170,750</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>1</td>
<td>2009/01/12</td>
<td>$86,000</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>1</td>
<td>2009/01/12</td>
<td>$86,000</td>
</tr>
</tfoot>
</table>