jquery:在偶数tr中更改表格行背景颜色

时间:2012-11-26 15:08:38

标签: jquery-selectors

我在php文件中有以下代码(部分):

<script type="text/javascript">
$(document).ready(function() {
    $(".MyYable tr:even").addClass("alt");
    jQuery("table tr").click(function(){
       var row = jQuery(this);
       var hasClass = row.hasClass("highlight");
       jQuery("table tr").removeClass("highlight");
       if(!hasClass){
         row.addClass("highlight");
       }
    });
    });
</script>

<style type="text/css">

.highlight{
background-color:#00FF00;
}
tr.alt td {
background:#CCCCCC;
}
tr.over td {
background-color:#00FF00;   
}
</style>

        <table id="Tabela_lista_empresas" width="751">
          <thead>
          <tr>
            <th width="300"><strong>Descrição</strong></th>
            <th width="100" align="center"><strong>Qtd</strong></th>
            <th width="100" align="center"><strong>Refª cliente</strong></th>
          </tr>
          </thead>

          <?php do {?>

          <tr <?php echo $i++ % 2 ? 'class="odd"' : ''; ?> onClick="get(<?php echo $row_rs_listaequipamentos['id_equipamento']; ?>, this)">
            <td width="300"><?php echo $row_rs_listaequipamentos['descricao_curta']; ?></td>
            <td width="100" align="center"><?php echo $row_rs_listaequipamentos['quantidade']; ?> <?php echo $row_rs_listaequipamentos['unidades']; ?></td>
            <td width="100" align="center"><?php echo $row_rs_listaequipamentos['referenciacliente']; ?></td>
          </tr>
          <?php } while ($row_rs_listaequipamentos = mysql_fetch_assoc($rs_listaequipamentos)); ?>
        </table>

在我的情况下,当我点击奇数行时,它会将类更改为“突出显示”,但如果我点击偶数行,则没有任何反应。 任何帮助表示赞赏。 注意:我正在努力学习“靠我自己”,所以我不是编程方面的专家。 感谢

1 个答案:

答案 0 :(得分:0)

为什么不使用css :hover选择器?

tr.over td {
background-color:#00FF00;   
}
tr.over td:hover {
background-color:#FFFF00;   
}