颜色为具有相同颜色的相同ID的线

时间:2019-09-19 15:30:54

标签: javascript html

我有一张下表,该表可以以不同或相等的状态返回具有相同id的多行,但使用不同的处理方式。我希望当您返回多个具有相同ID的行时,将它们全部涂成相同颜色。

<table class="table table-responsive" id="employee_table3"> 
<thead> 
<tr> 
<th>Data</th>
<th>Valência</th>
<th>Descrição</th>
<th>Colaborador</th>    
<th>Estado</th>
<th>Prestador de Serviços</th>
<th>Tratamento</th> 
<th>Data Tratamento</th>    
<th>Técnico</th>        
<th>Ação</th>   
<th>Eliminar</th>                               
</tr> 
</thead>
<tbody>
<?php  do{ ?>
<tr id="<?php echo $produto3["Id"]; ?>"> 
<td><?php echo $produto3["DataRegisto"]; ?></td> 
<td><?php echo $produto3["Destino"]; ?></td> 
<td><?php echo $produto3["Descricao"]; ?></td> 
<td><?php echo $produto3["nome"]; ?></td> 
<td><?php echo $produto3["Estado"]; ?></td>
<td><?php echo $produto3["Prestador"]; ?></td> 
<td><?php echo $produto3["Tratamento"]; ?></td> 
<td><?php echo $produto3["DataTermino"]; ?></td> 
<td><?php echo $produto3["Colaborador1"]; ?></td> 
<td><button type="button" name="edit3" data-id="<?php echo $produto3["Id"]; ?>"  data-target="#add_data_Modal3" class="btn btn-warning btn-sm edit_data3" ><span class="glyphicon glyphicon-pencil"></span></button></td>
<td><button class="btn btn-danger btn-sm remove3"><span class="glyphicon glyphicon-trash"></span></button></td>
</tr> 
<?php } while($produto3 = $result3->fetch_assoc()); ?>
</tbody>      
</table 

1 个答案:

答案 0 :(得分:1)

id必须是唯一的。示例:document.getelementbyid将获得第一个,而忽略其他人。也许您可以尝试使用class

// set same class name
<tr class="color_<?php echo $produto3["Id"]; ?>">

<style> .color_1 { color: red; } </style>
// or
document.querySelectorAll('.color_1').forEach(function(item){ item.style.color='red' });

编辑:

<table> 
<thead> 
<tr> 
<th>name</th>
<th> # </th>
</tr> 
</thead>
<tbody>
<?php  do{ // example 4 ?>
<tr class="<?="color_$produto3['Id'];"?>" style="color: <?php echo $produto3["Color"]; ?>" > 
<td>henry</td>
<td><button onclick="change_color('<?php echo $produto3["Color"]; ?>', '<?php echo $produto3["Id"]; ?>')">click me change</button></td>
</tr> 
<?php } while($produto3 = $result3->fetch_assoc()); ?>
</tbody>
</table>

// example 1 css
<style>
 .color_1 { color: red; }
 .color_2 { color: axuis; }
 .color_3 { color: <?=$color?>; } // defined by php 
 ... as so on
</style>

// example 2 javascript 
<script>
window.onload = function(){

  let id;
  let color_list = ['red', 'axuis' , ... ];
  for (id = 1; id < 10; id++) { //

    document.querySelectorAll('.color_'+ id).forEach(function(item){
        item.style.color =  color_list[id];
    });
  }
}

// example 3 : change by user click button

var change_color(color, id){
    document.querySelectorAll('.color_'+ id).forEach(function(item){
        item.style.color =  color_list[id];
    });
}

<script>