javascript - jquery get the last tr with a th - Stack Overflow

using jquery how do alter the css of the last tr with a th$("#mytable tr").has('th'

using jquery how do alter the css of the last tr with a th

$("#mytable tr").has('th').last().parents('tr').css({'color':'blue'});

HTML

<table border="1" id="mytable">
    <tr>
        <th>row 1, cell 1</th>
        <th>row 1, cell 2</th> 
    </tr>
    <tr> 
        <th>row 1, cell 1 - change this</th>
        <th>row 1, cell 2 - change this</th> 
    </tr>
    <tr>
        <td>row 2, cell 1</td>
        <td>row 2, cell 2</td>
    </tr>
</table>

using jquery how do alter the css of the last tr with a th

$("#mytable tr").has('th').last().parents('tr').css({'color':'blue'});

HTML

<table border="1" id="mytable">
    <tr>
        <th>row 1, cell 1</th>
        <th>row 1, cell 2</th> 
    </tr>
    <tr> 
        <th>row 1, cell 1 - change this</th>
        <th>row 1, cell 2 - change this</th> 
    </tr>
    <tr>
        <td>row 2, cell 1</td>
        <td>row 2, cell 2</td>
    </tr>
</table>
Share edited Sep 18, 2012 at 7:25 BoltClock 725k165 gold badges1.4k silver badges1.4k bronze badges asked Sep 18, 2012 at 7:15 Hello-WorldHello-World 9,55524 gold badges90 silver badges157 bronze badges 1
  • And what happens if you do this? And what do you expect to happen? – Nanne Commented Sep 18, 2012 at 7:17
Add a ment  | 

2 Answers 2

Reset to default 6

As an alternative to xdazz's answer, I'd offer:

$('tr:has(th):last').css('color','blue');

References:

  • :has() selector.

Find the last th, and its parent should be the last tr who has th.

$("#mytable th:last").parent().css('color', 'blue');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744912880a4600658.html

相关推荐

  • javascript - jquery get the last tr with a th - Stack Overflow

    using jquery how do alter the css of the last tr with a th$("#mytable tr").has('th'

    2天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信