我有一个从MYSQL数据库获取行的表
<table id="table1">
<?php
// Connect to database server
mysql_connect("localhost", "root", "asnaeb") or die (mysql_error ());
// Select database
mysql_query("SET NAMES `utf8`"); // UTF 8 support!!
mysql_select_db("scores") or die(mysql_error());
// SQL query
$strSQL = "SELECT * FROM latest";
// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);
// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array
while($row = mysql_fetch_array($rs)) {
// Write the value of the column FirstName (which is now in the array $row)
?>
<?php echo $row['Header'].""; ?>
<tr>
<td id='date'><?php echo $row['Date'].""; ?></td>
<td id='time'><?php echo $row['Time'].""; ?></td>
<td id='hometeam'><?php echo $row['HomeTeam'].""; ?></td>
<td id='score'><?php echo $row['Score'].""; ?></td>
<td id='awayteam'><?php echo $row['AwayTeam'].""; ?></td>
<td id='other'><?php echo $row['Other'].""; ?></td>
</tr>
<?php } mysql_close(); ?>
</table>我有两个css类,分别称为"A“和"B”,用于奇数行和偶数行
我目前通过用<tr class='<?php echo $row['Row'].""; ?>'>替换<tr>来实现这一点,并且在我的数据库表中有一个列"Row“,我在A或B中为偶数或奇数行添加该列……问题是,如果我想要删除或添加其中一个之间的新行,我将不得不更改另一个中的所有A和B。
我在另一个问题中看到了在javascript或jquery中实现这一点的许多方法,但对于包含TR的普通表,这不是我的情况……(尝试了其中一些脚本,但无法修复它)
所以我想要一个更简单的方法来做到这一点,甚至和奇数行,谢谢!
发布于 2012-06-01 22:10:59
您可以使用jQuery轻松地添加这些类,如下所示
$(function(){
$('#table1 tr:odd').addClass('A');
// for even
$('#table1 tr:even').addClass('B');
});发布于 2012-06-01 22:12:30
一劳永逸地使用CSS方式(没有内联类):
CSS中的
#table1 tr:nth-child(odd) td { background-color:#ebebeb }
#table1 tr:nth-child(even) td { background-color:#0000ff }超文本标记语言中的
<table id="table1">就是这样,不管你的表行是否被删除。
发布于 2012-06-01 22:14:36
为什么你不在你的while循环中使用模?这是一种比将类存储在数据库中更好的方法。:
$i = 0;
while($row = mysql_fetch_array($rs)) {
// Write the value of the column FirstName (which is now in the array $row)
?>
<?php echo $row['Header'].""; ?>
<tr class="<?php echo $i%2 == 0 ? "class_A" : "class_B" ; $i++;?>" >
<td id='date'><?php echo $row['Date'].""; ?></td>
<td id='time'><?php echo $row['Time'].""; ?></td>
<td id='hometeam'><?php echo $row['HomeTeam'].""; ?></td>
<td id='score'><?php echo $row['Score'].""; ?></td>
<td id='awayteam'><?php echo $row['AwayTeam'].""; ?></td>
<td id='other'><?php echo $row['Other'].""; ?></td>
</tr>
<?php } mysql_close(); ?>https://stackoverflow.com/questions/10851816
复制相似问题