【问题标题】:td:first-child doesnt work for dynamically generated tdtd:first-child 不适用于动态生成的 td
【发布时间】:2014-08-11 07:11:10
【问题描述】:

我无法让 td:first-child 为静态创建的表中动态创建的 <td> 工作。 我有以下静态标记

    <table class="tblgenInfo">
        @for (int i = 0; i < Model.Controls.Count(); i++)
        {
            if (Model.Controls[i].HtmlAttributes["section"].ToString() != "0")
            {
                continue;
            }

            <tr>
                @Html.HiddenFor(x => x.Controls[i].Type)
                @Html.HiddenFor(x => x.Controls[i].ID)
                @Html.EditorFor(x => x.Controls[i])


             </tr>
          }
        </table>

在上面的代码中,@EditorFor(x => x.Control[i]) 在&lt;td&gt; 内动态创建控件。 我有以下 CSS,如果 &lt;td&gt;s 是静态创建的,但不适用于动态创建的,请帮忙。

    .tblgenInfo  tr  td:first-child{
     width: 28%;
     }

谢谢

【问题讨论】:

  • 动态生成的是否附加到tr 作为第一个孩子?
  • 你能发布生成的 HTML 吗?
  • 如果在td之前的tr中生成了其他元素,则需要使用td:first-of-type
  • @singe31:谢谢!那确实有效。我是这个论坛的新手,所以如何将您的答案/评论设置为接受的答案。我在第一个“”之前生成了一些元素,从上面的代码中可以明显看出(@HiddenFor)
  • 我没有提供任何解决方案

标签: css


【解决方案1】:

由于在 td 之前的 tr 内生成了其他元素 - first-child 选择器将不起作用。

在您的情况下,您需要使用first-of-type 选择器。

所以你的代码变成了:

.tblgenInfo  tr  td:first-of-type { 
   width: 28%; 
}

【讨论】:

    【解决方案2】:

    也许你把你的css放在页脚中,以便在页面加载后调用样式?

    <footer>
    
    <p>your footer words</p>
    
    <style>
       .tblgenInfo  tr  td:first-child { width: 28%; }
     </style>
    
    </footer>
    

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签