English 中文(简体)
jquery: 3. 如果一栏的头盔有一个班级,则改变该栏的背景。
原标题:jquery: Change the background-colour of a column if its header has a class

I have the following jQuery that I m trying to use to change the background colour of a column where its header has a class.

So for example:

$( th ).each(function (i) {

            if ($(this).hasClass( headerSortUp ) || $(this).hasClass( headerSortDown )) {
                sortIndex = $(this).index();
            }
            $( tr ).each(function () {
                $( td:eq(  + sortIndex +  ) ).css( background-color ,  orange );
            });

        });

因此,如果我有这样一个表格:

<table>
<tr>
    <th class="headerSortDown">Header</th>
    <th>Header</th>
    <th>Header</th>
    <th>Header</th>
    <th>Header</th>
</tr>
<tr>
    <td>Text</td>
    <td>Text</td>
    <td>Text</td>
    <td>Text</td>
    <td>Text</td>
</tr>
<tr>
    <td>Text</td>
    <td>Text</td>
    <td>Text</td>
    <td>Text</td>
    <td>Text</td>
</tr>
<tr>
    <td>Text</td>
    <td>Text</td>
    <td>Text</td>
    <td>Text</td>
    <td>Text</td>
</tr>
</table>

因此,在这一例子中,整个第一栏应当有一 b。 但是,它没有工作...... 我已经做过错的任何想法? 增 编

最佳回答

页: 1

$( td:eq(  + sortIndex +  ) , this).css( background-color ,  orange );
问题回答

Try changing sortIndex = $(this).index(); to sortIndex =i; and
$( td:eq( + sortIndex + ) ).css( background-color , orange ); to

$(td:eq(+ lyIndex + ) ,$ (this)cs ( background-color , orange ); to selected td s with the tr

There s no need to iterate over the <th> elements - a selector can pick out just the ones with the right classes.

之后,请使用<代码>.index(),以找到其栏目价值;:nth-child(<>/code>,选择每一栏目<代码><td>

$( .headerSortUp, .headerSortDown ).each(function() {
    var n = $(this).index() + 1;
    $( td:nth-child(  + n +  ) ).css( background-color ,  orange );
});

Working demo at

[我还注意到,原法典有逻辑错误——更改颜色的代码应在<条码>内......>}栏内,而不是在其外面]。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签