[英]Find table header cell value (First Column)
I have a HTML table as listed in http://jsfiddle.net/Lijo/sP7zD/ . 我有一个http://jsfiddle.net/Lijo/sP7zD/中列出的HTML表。 I need to read the value of first column's header. 我需要读取第一列标题的值。 I am doing it with “gt” and “lt” operators. 我正在使用“ gt”和“ lt”运算符进行操作。 But it is not getting the first column value. 但是它没有获得第一列的值。
CODE 码
<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>
SCRIPT 脚本
$('.alertButton').click(function() {
var selectedElements = $("tr").find("th:gt(0):lt(1)");
$(selectedElements).css('background-color','yellow');
alert(selectedElements.html());
});
Use $('th:first')
使用$('th:first')
var selectedElements = $("th:first");
Here is the demo . 这是演示 。
For your code: change to use eq
instead. 对于您的代码:改为使用eq
。
var selectedElements = $("tr").find("th:eq(0)");
To answer question 1, your code tries to find the element with an index greater than 0, so it finds the second. 要回答问题1,您的代码将尝试查找索引大于0的元素,因此将查找第二个元素。 Try removing the gt
. 尝试删除gt
。 This will find the element with an index less than 1, so it will match the element with an index of 0. 这将找到索引小于1的元素,因此它将匹配索引为0的元素。
var selectedElements = $("tr").find("th:lt(1)");
But there are better ways of doing this, as mentioned in other answers. 但是,还有其他答案中提到的更好的方法。
Try this 尝试这个
HTML CODE 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 CODE JS代码
$('.alertButton').click(function()
{
var selectedElements = $('th').first();
alert(selectedElements .text());
selectedElements.css({'background':'yellow'});
});
Use var selectedElements = $(".resultGridTable").find("tr:first").find('th:first');
使用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.