html - 使用带有 jsf 重复标记的第 n 个子 css 选择器

标签 html css jsf repeat css-selectors

我试图让我的 html 页面与 JSF 一起工作,并且偶然发现了一个问题,即如何让 nth-child css 选择器与 jsf 一起工作 repeat标签?现在,对于 repeat 标签生成的每个元素,都会使用第一个 child 的 css。

这是纯 HTML:

            <table>
                        <tbody>
            <tr>
                                <td class="costs" >
                                    <ul>
                                        <li>25</li>
                                        <li>25</li>
                                        <li>25</li>
                                        <li>25</li>
                                        <li>25</li>
                                    </ul>
                                </td>
                            </tr>
            <tbody>
        <table>   

和 CSS:

.costs ul li:nth-child(1) {
    background-image: url("#{resource['images:icons/food.png']}");
}

.costs ul li:nth-child(2) {
    background-image: url("#{resource['images:icons/wood.png']}");
}

.costs ul li:nth-child(3){
    background-image: url("#{resource['images:icons/stone.png']}");
}

.costs ul li:nth-child(4) {
    background-image: url("#{resource['images:icons/iron.png']}");
}

.costs ul li:nth-child(5) {
    background-image: url("#{resource['images:icons/gold.png']}");
}

.costs ul li:nth-child(6) {
    background-image: url("#{resource['images:icons/time.png']}");
}

这是 JSF 对应物:

                <h:dataTable>    
             <h:column>
                        <div class="costs" >
                            <ul>
                                <ui:repeat value="#{item.goods}" var="goodType">
                                    <li>
                                        <h:outputText value="#{goodType}" ></h:outputText>                                                   
                                    </li>
                                </ui:repeat>
                            </ul>       
                        </div>
                    </h:column>
                </h:dataTable>

提前致谢!

最佳答案

我不知道jsf ,但我假设 <ui:repeat行正在创建一些 html 元素,这就是您的代码失败的原因。

看看这个example你应该走上正轨。

从示例中可以看出,如果li被包裹成 div它永远是:first-child:nth-child(1)在你的情况下。

所以你的代码应该与这个 CSS 一起工作:

.costs ul div:nth-child(1) li {
    background: red;
}

其中 div 是由 <ui:repeat 创建的 html 元素. Example

关于html - 使用带有 jsf 重复标记的第 n 个子 css 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20764631/

相关文章:

Javascript 克隆/附加会影响错误的图像或所有图像

javascript - 如何创建图标菜单?

javascript - :active vs mousedown event preventDefault()-ed

java - 为基于 JSF 的 Web 应用程序选择测试框架

html - 我可以仅使用 html 和 css 复制此图像吗?特别是形状和边缘吗?

javascript - 如何将 jQuery 脚本集成到我的 html 文件中?

html - 用 span 覆盖 100% 的 div 宽度

html - 将 "Pure CSS Sphere"实现到网站中 - 我该怎么做?

jsf - 在 jsf 托管 bean 中创建动态控件?

java - JSF 中 render=true/false 问题的奇怪问题