[英]Find table header cell value (First Column)
我有一个http://jsfiddle.net/Lijo/sP7zD/中列出的HTML表。 我需要读取第一列标题的值。 我正在使用“ gt”和“ lt”运算符进行操作。 但是它没有获得第一列的值。
码
<input type="submit" value="Alert" class="alertButton"/>
<table class="resultGridTable" cellspacing="0" id="detailContentPlaceholder_grdLocalTaxReport"
style="border-collapse: collapse;">
<tr>
<th scope="col">
IsSummaryRow
</th>
<th scope="col">
Associate
</th>
<th scope="col">
Gross Amount
</th>
<th scope="col">
Federal Withholding
</th>
</tr>
<tr>
<td>
False
</td>
<td>
Norman Tylor
</td>
<td>
3450
</td>
<td>
32
</td>
</tr>
</table>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.1.js"></script>
脚本
$('.alertButton').click(function() {
var selectedElements = $("tr").find("th:gt(0):lt(1)");
$(selectedElements).css('background-color','yellow');
alert(selectedElements.html());
});
使用$('th:first')
var selectedElements = $("th:first");
这是演示 。
对于您的代码:改为使用eq
。
var selectedElements = $("tr").find("th:eq(0)");
要回答问题1,您的代码将尝试查找索引大于0的元素,因此将查找第二个元素。 尝试删除gt
。 这将找到索引小于1的元素,因此它将匹配索引为0的元素。
var selectedElements = $("tr").find("th:lt(1)");
但是,还有其他答案中提到的更好的方法。
尝试这个
HTML代码
<input type="submit" value="Alert" class="alertButton" />
<table class="resultGridTable" cellspacing="0" id="detailContentPlaceholder_grdLocalTaxReport"
style="border-collapse: collapse;">
<tr>
<th scope="col">
IsSummaryRow
</th>
<th scope="col">
Associate
</th>
<th scope="col">
Gross Amount
</th>
<th scope="col">
Federal Withholding
</th>
</tr>
<tr>
<td>
False
</td>
<td>
Norman Tylor
</td>
<td>
3450
</td>
<td>
32
</td>
</tr>
</table>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.1.js">
</script>
JS代码
$('.alertButton').click(function()
{
var selectedElements = $('th').first();
alert(selectedElements .text());
selectedElements.css({'background':'yellow'});
});
使用var selectedElements = $(".resultGridTable").find("tr:first").find('th:first');
$('.alertButton').click(function()
{
var selectedElements = $(".resultGridTable").find("tr:first").find('th:first');
$(selectedElements).css('background-color','yellow');
alert(selectedElements.html());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.