gpt4 book ai didi

javascript - 颜色为具有相同颜色的相同 ID 的线条

转载 作者:行者123 更新时间:2023-12-02 22:55:15 25 4
gpt4 key购买 nike

我有下表,它可以在不同或相等的状态下返回具有相同 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

最佳答案

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>

关于javascript - 颜色为具有相同颜色的相同 ID 的线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58014463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com