通过了解父类来定位子id

时间:2013-09-11 16:49:29

标签: css

我有一个有几个孩子的css类,我知道我想要定位的子元素有一个以“inner-Ct结尾的id。我不想将它分配给我唯一的ID,因为它将在很多地方使用。

<div class="totalSummary">
    <div>
        <div id = "form-1406-innerCt"></div> //<---- this is the one I want to target
          ...
    <div>
</div>

有没有办法用css做到这一点?

2 个答案:

答案 0 :(得分:1)

您可以使用$属性选择器来匹配ID的结尾,如:

.totalSummary div[id$="innerCt"] {
    background: red;
}

<强> jsFiddle example

请参阅:http://www.w3.org/TR/selectors/#selectors

  

[att $ = val]表示具有att属性值的元素   以后缀“val”结尾。如果“val”是空字符串那么   选择器不代表任何东西。

答案 1 :(得分:0)

您可以使用以下内容:

.totalSummary div[id$="innerCT"] {
    color: gold;  /* The $ indicates a string that an attribute
                     value ends with, in this case "innerCt" */
}

示例:http://jsfiddle.net/xgRk7/