最简单的方法交替行颜色在PHP / HTML? [英] Easiest way to alternate row colors in PHP/HTML?
本文介绍了最简单的方法交替行颜色在PHP / HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的一个PHP示例。任何人都可以找到一个更短/更简单的方法来做到这一点?
Here's a PHP example of mine. Can anyone find a shorter/easier way to do this?
<? foreach($posts as $post){?>
<div class="<?=($c++%2==1)?‘odd’:NULL?>">
<?=$post?>
</div>
<? }?>
<style>
.odd{background-color:red;}
</style>
其他语言的示例也很有趣。
Examples in other languages would be fun to see as well.
推荐答案
从根本上 - 没有。这就是那么容易。你可以重写它有点更短/更清洁,但想法会一样。这是我写的:
Fundamentally - no. That's about as easy as it gets. You might rewrite it a bit shorter/cleaner, but the idea will be the same. This is how I would write it:
$c = true; // Let's not forget to initialize our variables, shall we?
foreach($posts as $post)
echo '<div'.(($c = !$c)?' class="odd"':'').">$post</div>";
这篇关于最简单的方法交替行颜色在PHP / HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文