admin管理员组文章数量:1289391
I have a table where TD has no id however I have unique TH value. Example table is,
<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>
I want to read the value of Lead Time in a var. How can I read is using javascript i.e. var= ??
I have a table where TD has no id however I have unique TH value. Example table is,
<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>
I want to read the value of Lead Time in a var. How can I read is using javascript i.e. var= ??
Share Improve this question edited Oct 20, 2015 at 9:09 Mike 4,5704 gold badges34 silver badges48 bronze badges asked Oct 20, 2015 at 8:30 Sheikh SiddiqueeSheikh Siddiquee 2433 gold badges4 silver badges14 bronze badges4 Answers
Reset to default 6Use :contains()
Selector to filter the content of TH
and select next element to it using .next()
like so:
var lt = $('th:contains("Lead Time")').next().text();
Well you can do it as simply as:
var e = $(".even").find("td").text();
alert(e);
You can get all <td>
elements and get the required one by index
var cells = document.getElementById('product-attribute-specs-table').getElementsByTagName('td');
alert(cells[2].innerHTML );
<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>
You can use nth-child
selector like so:
$(document).ready(function(){
var leadTime = $("table tbody tr:nth-child(3) td").html();
console.log(leadTime);
});
<script src="https://ajax.googleapis./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>
本文标签: jqueryHow to get a TD value using JavaScript when TD does not contain any IDStack Overflow
版权声明:本文标题:jquery - How to get a TD value using JavaScript when TD does not contain any ID - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741392750a2376197.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论