使用 XSL 在 tr 类中交替行颜色 [英] Alternating row colours in a tr class using XSL

查看:19
本文介绍了使用 XSL 在 tr 类中交替行颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XSL 文档,其中插入了可变数量的文章.我需要文章的背景颜色交替 - 奇数"然后偶数"

I have a XSL document which has a varaible number of articles inserted into it. I need the background colours of the articles to alternate - "Odd" then "even"

<xsl:for-each select="newsletter/section/article">
    <tr class="odd" style="background-color: #efefef;">
        <td valign="top">
            <xsl:element name="a">
                <xsl:attribute name="href">
                    <xsl:value-of select="link" />
                </xsl:attribute>
                <img align="left" valign="top" width="110" 
                            style="padding: 0 4px 4px 0; border:0;">
                    <xsl:attribute name="alt">
                        <xsl:value-of select="title" />
                    </xsl:attribute>
                    <xsl:attribute name="src">
                        <xsl:value-of select="img" />
                    </xsl:attribute>
                </img>
            </xsl:element>
        </td>
        <td valign="top" style="padding: 4px 4px 18px 0;">
            <strong>
                <xsl:element name="a">
                    <xsl:attribute name="href">
                        <xsl:value-of select="link" />
                    </xsl:attribute>
                    <xsl:value-of select="title"/>
                </xsl:element>
            </strong>
            <br />
            <xsl:value-of select="excerpt"/>
        </td>
    </tr>
</xsl:for-each>

我看过这篇文章:具有交替行颜色的 HTML 表格通过XSL

但我相信我的情况不同.我只需要在每次迭代时更改 tr 类.抱歉,奇怪的格式,我似乎在这里在 Chrome 中粘贴代码时遇到问题.

but my case is different I believe. I just need to change the tr class on each iteration. Sorry for the weird formatting, I seem to be having problems pasting code in Chrome on here.

推荐答案

使用:

<xsl:for-each select="newsletter/section/article">
  <xsl:variable name="vColor">
    <xsl:choose>
      <xsl:when test="position() mod 2 = 1">
        <xsl:text>#efefef</xsl:text>
      </xsl:when>
      <xsl:otherwise>#ababab</xsl:otherwise>
        </xsl:choose>
      </xsl:variable>


  <tr class="odd" style="background-color: {$vColor};">

这篇关于使用 XSL 在 tr 类中交替行颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆