引用tr元素

时间:2013-04-11 19:53:37

标签: jquery

我是jquery的新手,所以如果这是一个愚蠢的问题,我道歉......

$('#sortList tr').each(function()
{
    i = $(this).index();
    v = $(this).find('td:first').text();
    n = $(this).find('td:first').html();  
});

循环遍历表的行并获取每行的索引以及每行的第一个单元格的文本和html。一切正常。然后我尝试在下一行得到相同的结果,我发现以下内容......

这很好...... j = $(this).next().index();

但这失败了...... v = $(this).next().find('td:first').text();

这失败了...... n = $(this).next().find('td:first').html();

如果$(this)$(this).next()都适用于index(),为什么不find()?它们不是指向tr的指针吗?

<table border="0" cellspacing="0" cellpadding="0" id="sortList" class="pagetext" width="200">
    <tr>
        <td align='left'><a href="Javascript:;;" OnClick="Javascript:Link_OnClick('ACCRUAL','Accrual');return false;">Accrual</a></td>
    </tr>
    <tr>
        <td align='left'><a href="Javascript:;;" OnClick="Javascript:Link_OnClick('EETAXES','Employee - Taxes');return false;">Employee - Taxes</a></td>
    </tr>
    <tr>
        <td align='left'><a href="Javascript:;;" OnClick="Javascript:Link_OnClick('EMPVOLDEDS','Employee - Voluntary Deductions');return false;">Employee - Voluntary Deductions</a></td>
    </tr>
    <tr>
        <td align='left'><a href="Javascript:;;" OnClick="Javascript:Link_OnClick('ERMATCH','Employer - 401(k) Match');return false;">Employer - 401(k) Match</a></td>
    </tr>
    <tr>
        <td align='left'><a href="Javascript:;;" OnClick="Javascript:Link_OnClick('ERTAXES','Employer - Taxes');return false;">Employer - Taxes</a></td>
    </tr>
    <tr>
        <td align='left'><a href="Javascript:;;" OnClick="Javascript:Link_OnClick('GTL','Group Term Life');return false;">Group Term Life</a></td>
    </tr>
    <tr>
        <td align='left'><a href="Javascript:;;" OnClick="Javascript:Link_OnClick('MISC','Miscellaneous');return false;">Miscellaneous</a></td>
    </tr>
    <tr>
        <td align='left'><a href="Javascript:;;" OnClick="Javascript:Link_OnClick('NETPAY','Net Payroll');return false;">Net Payroll</a></td>
    </tr>
    <tr>
        <td align='left'><a href="Javascript:;;" OnClick="Javascript:Link_OnClick('TAXFILEE','Tax Filing - Employee Taxes');return false;">Tax Filing - Employee Taxes</a></td>
    </tr>
    <tr>
        <td align='left'><a href="Javascript:;;" OnClick="Javascript:Link_OnClick('TAXFILER','Tax Filing - Employer Taxes');return false;">Tax Filing - Employer Taxes</a></td>
    </tr>
    <tr>
        <td align='left'><a href="Javascript:;;" OnClick="Javascript:Link_OnClick('TPCORTAX','Third Party Checks or Tax Checks');return false;">Third Party Checks or Tax Checks</a></td>
    </tr>
    <tr>
        <td align='left'><a href="Javascript:;;" OnClick="Javascript:Link_OnClick('TPS','Third Party Sick');return false;">Third Party Sick</a></td>
    </tr>
    <tr>
        <td align='left'><a href="Javascript:;;" OnClick="Javascript:Link_OnClick('WAGE','Wage');return false;">Wage</a></td>
    </tr>
</table>

1 个答案:

答案 0 :(得分:0)

不确定您的代码有什么问题,但这个整理的版本有效:

$('#sortList tr').each(function(rowIndex) {
    var $row = $(this),
        $nextRow = $row.next(),
        $td, j, v, n;

    $td = $row.find('td:first');
    j = rowIndex;
    v = $td.text();
    n = $td.html();

    console.log('this row: ' + j + '\t' + v + '\t' + n);

    if ($nextRow.length > 0) {
        $td = $nextRow.find('td:first');
        j = rowIndex + 1; // duh?
        v = $td.text();
        n = $td.html();
        console.log('next row: ' + j + '\t' + v + '\t' + n);
    }

});

http://jsfiddle.net/VxSZf/2/