如何在表格中选择第二个元素?

时间:2014-06-05 15:31:27

标签: javascript jquery html

我创建了以下代码(工作正常),它添加了一个类来排序图标和单击一列。这是一个网格修复。

以下代码:

    /* Update sort icon (Ascending or descending) based on saved sort setting */
    updateSortIcon: function (containerConfiguration) {
    /* update sort direction icon based on stored sort setting; Sort setting is stored in the database */
    var classElement = $('td[class$=' + containerConfiguration.GridReportInfo.SortingInfo.AnalyticUniqueId + ']');
    if (this.validChildElement(classElement)) {
        var indicatorElement = classElement.next()[0].children[0];
        var newIndicatorClass;
        var newParentIndicatorClass;
        switch (containerConfiguration.GridReportInfo.SortingInfo.SortDirection) {
            case ('ASC'):
            case 0:
                newIndicatorClass = 'GOSort1Right GOImage column-bg-color-asc';
                newParentIndicatorClass = 'column-bg-color-asc';
                break;
            case ('DESC'):
            case 1:
                newIndicatorClass = 'GOSort4Right GOImage column-bg-color-desc';
                newParentIndicatorClass = 'column-bg-color-desc';
                break;
            default:
                newIndicatorClass = 'GOSort0Right GOImage';
                newParentIndicatorClass = '';
                break;
        }
        classElement.first().removeClass('column-bg-color-desc column-bg-color-asc').addClass(newParentIndicatorClass);
        indicatorElement.setAttribute('class', newIndicatorClass);
    }
},

此代码将 column-bg-color-asc 应用于第一个表格单元格和图标,但如何将 column-bg-color-asc 类添加到第二个表格单元格?

<td id="ext-gen1559" class="GOWrap0 GOAlignCenter GOHeaderText GOCellHeader GONoRight HideCol0ext-gen28361395166306 column-bg-color-asc">No. of Positions</td>



<td class=" GOCellHeader GONoLeft GOAlignRight GOHeaderButton"> 

<u class="GOSort1Right GOImage column-bg-color-asc"></u>
</td>

1 个答案:

答案 0 :(得分:1)

如果它是每行中的第二个单元格,那么:

$("#tableId> tbody > tr").each(function() { 
$(this).find("td:nth(1)").addClass('column-bg-color-asc');
});

上面的代码获取表行。然后迭代每一行并获得第二个单元格。在那个单元格上它添加了类。