【问题标题】:Nth-child applying to every buttonNth-child 应用于每个按钮
【发布时间】:2014-10-23 03:13:37
【问题描述】:

我正在尝试使用以下 css 对每个其他按钮执行不同的样式:

tr td:nth-child(odd) button {
   background-color: red;
}

但是,每个按钮都是红色的,并且它们都具有相同的 tr td:nth-child(odd) button {} 样式。 这些按钮是在 g:each 语句中“创建”的:

<table class="table table-striped">
<g:each in="${exactMatches}">
 <tr>
   <td>
    <!-- Button trigger modal -->
    <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#modal${it.id}">
        ${it.name}
    </button>
    .
    . more stuff that appears in modal...
    .
  </td>
</tr>

【问题讨论】:

    标签: jquery html css css-selectors


    【解决方案1】:

    看起来你所有的按钮都在一个 TD 中,所以你需要将你的 css 更改为:

    tr td button:nth-child(odd) {
       background-color: red;
    }
    

    【讨论】:

    • @Zav 这不起作用,我更新了我的代码以显示更多结构
    • 也尝试测试 - background-color: red !important;
    • “编译”的 html 是什么意思,!important 没有区别。
    • 已编译 = 在浏览器中呈现。如果你使用 firefox,你可以使用插件“web developer” -> 查看源代码 -> 查看生成的源代码
    • 我用的是google chrome,可以查看源码。有点长,怎么放在这里?
    【解决方案2】:

    中继器正在创建这样的结构(显然):

    <tr>
        <td>
            <button></button>
        </td>
    </tr>
    <tr>
        <td>
            <button></button>
        </td>
    </tr>
    ...
    

    所以,nth-child 选择器应该在 tr 元素上,因为每个 tr 上只有一个 td

    tr:nth-child(odd) td button {
       background-color: red;
    }
    

    【讨论】:

      【解决方案3】:

      删除css中的按钮

      td button:nth-child(odd) {
         background-color: red;//whatever colr
      }
      

      【讨论】:

      • 可以使用 button:nth-child(2) 吗??
      • 仍然所有按钮都是红色的
      • button:nth-child(2) 根本不适合我
      • 是否可以在 jsfiddle 或相关代码中添加所有 css html 和 js,以便我可以正确测试它。 URL 也可能有帮助。
      • 我以前从未使用过 jsfiddle。这段 html 也是一个正在渲染的模板,所以我不确定它是否会成功。此外,我的 javascript 函数对控制器操作进行 ajax 调用,然后使用我设置的服务来获取结果。
      猜你喜欢
      • 2023-03-13
      • 2018-07-30
      • 2013-04-04
      • 2017-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      相关资源
      最近更新 更多