简体   繁体   中英

How to set a css class to table header cell

I have a table with thead cells. I want to set a css Class to 'th' only where the value of this 'th' is "ID".

I tried this :

var T = [];
$('.tbl > thead > tr > th').each(function(){
    T.push($(this).text())
 })
for (var i = 0; i < T.length; i++) {
    if (T[i] == "ID" ) {$('.tbl thead tr th').addClass('Myclass')};
}

But this Code set the class 'Myclass' to all headers of my table.

<thead> 
    <tr>  
        <th class="Myclass">ID</th> 
        <th class="Myclass">NOm</th>
    </tr>
</thead>

here is a fiddle --> http://jsfiddle.net/1ck4joum/1/

Thanks.

用这个

$('.tbl thead tr th:contains("ID")').addClass('Myclass')};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM