2012-11-26 61 views
0

我有下面的代碼在PHP文件(的一部分):jQuery的:在一個變化錶行的背景顏色,甚至TR

<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> 

在我而言,當我點擊一個奇數行,它會改變該班級「突出顯示」,但如果我點擊偶數行沒有任何反應。 任何幫助表示讚賞。 注意:我試圖學習「我自己」,所以我不是編程方面的專家。 謝謝

回答

0

爲什麼不使用css :hover選擇器?

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

好問題,它是更好的像你說的,但你能幫我改變我的代碼的效果嗎?謝謝。 –

+0

失去jquery並添加我的css示例。然後在閱讀':hover'上的文檔後調整它的顏色 –