我有一些 HTML,如下︰
<table id="resultsTable" class="table table-bordered table-responsive table-hover table-condensed sortable">
<thead>
<tr>
<th>Company Name</th>
<th>Tours Offered</th>
<th>Average Rating</th>
<th>Total Reviews</th>
</tr>
</thead>
<tbody class="searchable">
@foreach (var item in Model.AccommodationList)
{
<tr>
<td class="accommodationName">
@Html.ActionLink(item.AccommodationName, "ViewHomePage", "AccommodationHomepage", new {accommodationId = item.AccommodationId}, null)
</td>
<td>
@Html.DisplayFor(modelItem => item.FormattedAddress)
</td>
<td>
<Deleted for brevity>
</td>
<td>
@Html.DisplayFor(modelItem => item.TotalReviews)
</td>
<td class="latitudeCell" style="display: none;">
@Html.DisplayFor(modelItem => item.Latitude)
</td>
<td class="longitudeCell" style="display: none;">
@Html.DisplayFor(modelItem => item.Longitude)
</td>
</tr>
}
</tbody>
</table>
我试图在每行的下面的 jQuery 获取的住宿名称、 纬度和经度值︰
$('#resultsTable tbody tr').each(function () {
var latitude = $(this).find(".latitudeCell").html();
var longitude = $(this).find(".longitudeCell").html();
var accommodationName = $(this).find(".accommodationName").html();
});
}
不过我必须要做错什么了因为我不能得到任何值。
使用 javascript table.rows
函数和 textContent
属性来获取单元格的内部文本,像你能做某事如下︰
for(var i = 1, row; row = table.rows[i]; i++)
{
var col1 = row.cells[0].textContent;
var col2 = row.cells[1].textContent;
} var col3 = row.cells[2].textContent;
不要使用的 innerText 它比 textContent 慢得多,也不工作在火狐浏览器。