django table tr 奇偶行

Method 1: The Cross-Browser CSS Way

The easiest way to do this is to make use of the built-in Django {% cycle %} tag. Here’s how to use it for a table containing blog entries:

<table>
<tbody>
{% for blog in blogs %}
  {% for entry in blog.entries %}
    <tr class="{% cycle 'odd' 'even' %}">
      {{entry.date}}
      {{entry.title}}
      {{entry.comments}}
    </tr>
  {% endfor %}
{% endfor %}
</tbody>
</table>

Method 2: The Pure CSS Way

tbody tr:nth-child(even) td {background: #bbeebb;}
tbody tr:nth-child(odd) td {background: #e5f9e5;}


comments powered by Disqus