簡體   English   中英

交替的行顏色

[英]alternating row colors

我正在尋找一個解決方案,使用我的表中的交替顏色行設計,來自csv。

我的代碼:

<?php
echo "<table style='text-align: left; width: 99%;' border='1'>\n\n
<thead>
<tr>
<th>data</th>

</tr>
</thead>";
$f = fopen("local/datafiles.csv", "r");
while (($line = fgetcsv($f)) !== false) {
        echo "<tr class='odds'>";
        foreach ($line as $cell) {
                      echo "<td style='font-weight: bold;'>" . htmlspecialchars($cell) . "</td>";
        }
        echo "</tr>\n";
}
fclose($f);
echo "\n</table>";
?>

那么我怎么能讓tr類從賠率甚至? 謝謝

有一個變量

int count = 0;

每次迭代時遞增計數並采用模態

數%2

if(count % 2 == 0)
color = red //(make color of row = red)

else
color = yellow //(make color of row = yello)

count=count+1;

這只是一個想法,你可以在你的代碼中容納這個

<?php
echo "<table style='text-align: left; width: 99%;' border='1'>\n\n
<thead>
<tr>
<th>data</th>

</tr>
</thead>";
$f = fopen("local/datafiles.csv", "r");
$i = 0;
while (($line = fgetcsv($f)) !== false) {
    echo "<tr style='background-color: ".(($i%2)?'green':'blue').";'>";
    foreach ($line as $cell) {
        echo "<td style='font-weight: bold;'>" . htmlspecialchars($cell) . "</td>";
    }
    echo "</tr>\n";
    $i++;
}
fclose($f);
echo "\n</table>";
?>

或者您可以使用css以相同的方式分配類並對其進行樣式設置。 這是更好的做法。

你可以使用css偽選擇器:nth-​​of-type(odd | even)

http://reference.sitepoint.com/css/pseudoclass-nthoftype

如果表的ID是styledTable,則樣式表將如下所示:

#styledTable {
 text-align: left;
 width: 99%;
 border: 1px solid black;
}

#styledTable tr:nth-of-type(even) {
 background-color: red;
}

#styledTable tr:nth-of-type(odd) {
 background-color: blue;
}

我將通過CSS為您提供更好但更簡單的解決方案。 首先,唯一標識您的表格,我將向您展示更一般的選項。

table tr { backgroun: #ddd; }
table tr:nth-child(2n) { background: #ccc; } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM