Versuchen Sie, die .eq()
Methode das gewünschte Element, um es auszuwählen Index bereitstellt, und .text()
Methode, um es Text ist.
Überprüfen Sie das Beispiel:
function getFirstCellTextList(tableElement) {
if (tableElement instanceof jQuery) {
// Create an array
var textList = [];
// Iterate over each table row
tableElement.find('tbody tr').each(function() {
// Get the first cells's text and push it inside the array
var row = $(this);
if (row.children('td').length > 0) {
textList.push(row.children('td').eq(0).text());
}
});
return textList;
}
}
// Get the array
var myList = getFirstCellTextList($('#your_table'));
alert(myList);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="your_table">
<thead>
<tr>
<th>First</th>
<th>Senond</th>
<th>Third</th>
</tr>
</thead>
<tbody>
<tr><td>First cell 1</td><td>Second cell 1</td><td>Third cell 1</td></tr>
<tr><td>First cell 2</td><td>Second cell 2</td><td>Third cell 2</td></tr>
<tr><td>First cell 3</td><td>Second cell 3</td><td>Third cell 3</td></tr>
<tr><td>First cell 4</td><td>Second cell 4</td><td>Third cell 4</td></tr>
</tbody>
</table>
jQuery bietet eine Funktion für diese zu bekommen: http://api.jquery.com/jquery.each/ – nbokmans
Nicht sicher, was Sie fragen : '$ (" td "). first(). text()'? –
Ich kann den Text für diese spezifische Td erhalten, wenn das Modell nur 1 Datensätze enthält. aber was ist wenn ich mehr davon habe? –