【问题标题】:jquery set color of one table rowjquery设置一个表格行的颜色
【发布时间】:2012-05-20 22:58:30
【问题描述】:

我需要能够将 jquery 中单个表格行的颜色更改为红色,并将列出的所有其他行设置为白色。

我为每个人设置了一个类,例如<tr class=row1><tr class=row2>。 应该着色的当前行 id 的 javascript 变量是 id

我从这里去哪里?

【问题讨论】:

  • 在你澄清你的问题之前不要去任何地方,编辑你的问题以获得答案。
  • 你需要澄清你的问题!

标签: jquery colors


【解决方案1】:

jsBin demo

$('table tr').eq(  ).css({color:'red'}).siblings().css({color:'white'});
//               ^^-- set here the index number of the row you desire to change color

【讨论】:

    【解决方案2】:

    我建议使用 css,

    例如如果您想将 class="row1" 的第 3 行设置为红色,

     .row1:nth-child(3){
         background-color: red;
      }
    

    【讨论】:

      【解决方案3】:
      $('tr:eq(0)', 'table')
          .siblings('color', '#fff') // select all tr except first one and change color
          .andSelf() // target to the first one
          .css('color', '#f00'); ​​​// change color of first tr
      

      您可以根据您的目标元素更改:eq(index) 内的索引。

      DEMO

      【讨论】:

      • 谢谢,我将如何指定我希望在哪个特定表上发生这种情况?
      • @user1022585 你可以使用$.each()遍历每个表。
      猜你喜欢
      • 2012-05-21
      • 2016-05-04
      • 1970-01-01
      • 2012-10-06
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多