查找表头单元格值(第一列)

时间:2012-09-19 08:09:25

标签: javascript jquery

我有http://jsfiddle.net/Lijo/sP7zD/中列出的HTML表格。我需要读取第一列标题的值。我正在使用“gt”和“lt”运算符。但它没有获得第一列值。

  1. 我在编写的脚本中需要做什么jQuery代码更改?
  2. 读取第一列标题值的更好方法是什么?
  3. 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());
    });
    

4 个答案:

答案 0 :(得分:3)

使用$('th:first')

var selectedElements = $("th:first");

Here is the demo

对于您的代码:改为使用eq代替。

var selectedElements = $("tr").find("th:eq(0)");

答案 1 :(得分:1)

要回答问题1,您的代码会尝试查找索引大于0的元素,因此会找到第二个元素。尝试删除gt。这将找到索引小于1的元素,因此它将匹配索引为0的元素。

var selectedElements = $("tr").find("th:lt(1)");

但正如其他答案所述,有更好的方法可以做到这一点。

答案 2 :(得分:1)

试试这个

HTML 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>

JS CODE

$('.alertButton').click(function()
    {                                                 
        var selectedElements = $('th').first();
            alert(selectedElements .text());
        selectedElements.css({'background':'yellow'});                 
});

DEMO

答案 3 :(得分:0)

使用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());


});