我有一张TD没有id的表,但我有唯一的TH值。示例表是,
<table class="data-table" id="product-attribute-specs-table">
<colgroup>
<col width="25%">
<col>
</colgroup>
<tbody>
<tr class="first odd">
<th class="label">SKU</th>
<td class="data last">904532</td>
</tr>
<tr class="odd">
<th class="label">Width</th>
<td class="data last">20</td>
</tr>
<tr class="even">
<th class="label">Lead Time</th>
<td class="data last">49</td>
</tr>
<tr class="last odd">
<th class="label">Depth</th>
<td class="data last">63</td>
</tr>
</tbody>
</table>
我想在var中读取提前期的值。我如何阅读使用javascript即var = ??
答案 0 :(得分:6)
使用:contains()
选择器过滤TH
的内容,并使用.next()
选择下一个元素,如下所示:
var lt = $('th:contains("Lead Time")').next().text();
答案 1 :(得分:0)
嗯,你可以这样做:
var e = $(".even").find("td").text();
alert(e);
答案 2 :(得分:0)
您可以获取所有<td>
个元素,并通过index
var cells = document.getElementById('product-attribute-specs-table').getElementsByTagName('td');
alert(cells[2].innerHTML );
&#13;
<table class="data-table" id="product-attribute-specs-table">
<colgroup><col width="25%">
<col>
</colgroup><tbody>
<tr class="first odd">
<th class="label">SKU</th>
<td class="data last">904532</td>
</tr>
<tr class="odd">
<th class="label">Width</th>
<td class="data last">20</td>
</tr>
<tr class="even">
<th class="label">Lead Time</th>
<td class="data last">49</td>
</tr>
<tr class="last odd">
<th class="label">Depth</th>
<td class="data last">63</td>
</tr>
</tbody>
</table>
&#13;
答案 3 :(得分:0)
您可以使用nth-child
选择器,如下所示:
$(document).ready(function(){
var leadTime = $("table tbody tr:nth-child(3) td").html();
console.log(leadTime);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table class="data-table" id="product-attribute-specs-table">
<colgroup><col width="25%">
<col>
</colgroup><tbody>
<tr class="first odd">
<th class="label">SKU</th>
<td class="data last">904532</td>
</tr>
<tr class="odd">
<th class="label">Width</th>
<td class="data last">20</td>
</tr>
<tr class="even">
<th class="label">Lead Time</th>
<td class="data last">49</td>
</tr>
<tr class="last odd">
<th class="label">Depth</th>
<td class="data last">63</td>
</tr>
</tbody>
</table>
&#13;