javascript - :last for table

标签 javascript jquery html css

  <table>

    <tr><td>First Row</td><td>2 Row</td><td>1 last</td></tr>
    <tr><td>Middle Row</td><td>2 Row</td><td>2 last</td></tr>
    <tr><td>Last Row</td><td>2 Row</td><td>3 last</td></tr>

  </table>


$('tr').each(function(index) {
    $("td:last").css({backgroundColor: 'yellow', fontWeight: 'bolder'});
  });

直播:http://jsfiddle.net/DxVG5/1/

为什么这只对最后 3 个有效,但对最后 1 个和最后 2 个无效?我该如何解决?

最佳答案

您的示例将获取每个 tr 中的最后一个 td,因为使用 :last 字面上获取 table< 中的最后一个 td/。试试这个:

$('tr td:last-child').css({
   backgroundColor: 'yellow', 
   fontWeight: 'bolder'
});

关于javascript - :last for table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7470665/

相关文章:

php - 如何在laravel的 Blade 文件中显示最新的('updated_at')记录

javascript - 如何在 vanilla Javascript 中创建、添加自定义属性和触发自定义事件

html - 如何在 float :left element to stop next element to move position if current element changes size? 之间设置一个固定间隙

javascript - Google Analytics 事件未触发包含命中类型

javascript - 加载延迟的逐个字母动画

jquery 如何循环遍历一系列项目并采取行动?

javascript - 使用 for 循环处理 ajax 问题

javascript - jquery .height() 在刚刚调用的追加函数之后没有返回真实值

javascript - Mootools 设置单选按钮已选中

php - 在我的 php 页面上禁用重新加载和 f5