每行具有不同文本颜色的乘法

我只是创建了乘法表本身,但我想不出一种方法来更改每一行的文本颜色,我是 php 的新手,我试过到处搜索,但我发现的只是改变每行的背景颜色,而不是文本......任何帮助将不胜感激!


这是我的代码:


function multiplication_table($rows, $cols){

                echo "<table width =\"500\" height=\"500\" border=\"50\">";

                    for ($row = 1; $row <= $rows; $row++){

                        echo'<tr>';

                    for ($col = 1; $col <= $cols; $col++)

                        echo '<td align = "center">' .$col*$row.'</td>';

                        echo '</tr>';

                    }   

                echo"</table>";

            }

如果您有一组与我上面发布的代码不同的代码,请教我!我会尽力去理解一切。


青春有我
浏览 139回答 2
2回答

鸿蒙传说

您必须为任何行添加类并为此类设置颜色function multiplication_table($rows, $cols){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<table width =\"500\" height=\"500\" border=\"50\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ($row = 1; $row <= $rows; $row++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo'<tr class="row'+$row+'">';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ($col = 1; $col <= $cols; $col++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<td align = "center">' .$col*$row.'</td>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '</tr>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo"</table>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }并在 css 中设置颜色,例如.row1 , .row1 td&nbsp;{&nbsp; &nbsp;background:red;}

慕标琳琳

您可以添加background color到行:function multiplication_table($rows, $cols){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<table width =\"500\" height=\"500\" border=\"50\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ($row = 1; $row <= $rows; $row++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Generate Random Color&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $color = "#".substr(md5(rand()), 0, 6);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Apply color to row&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo'<tr style="background-color:'.$color.'">';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ($col = 1; $col <= $cols; $col++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<td align = "center">' .$col*$row.'</td>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '</tr>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo"</table>";&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP