CSS选择器 - 根据项目的内容选择项目

时间:2014-09-05 07:07:47

标签: css css3

有没有办法只使用CSS选择一个基于它包含的内容的项目?例如,我正在寻找这样的东西:

div:contains(#childDivId) {
    // some code
}

非常感谢你的帮助

干杯

3 个答案:

答案 0 :(得分:2)

这通常称为“父选择器”,它在CSS中不存在。即使:has() selector的未来计划也表明it will only work as a selector from inside JavaScriptit's not included in the "fast profile"这一事实证明了这一点。所以你需要JS,简单明了。

答案 1 :(得分:1)

不幸的是css3还没有这样的东西。但是CSS4得到了:has()选择器。 http://www.w3.org/TR/selectors4/

答案 2 :(得分:0)

不幸的是CSS / CSS3中没有父选择器。

  • css3 选择器"通用兄弟组合器"也许可以用于你想要的东西:
  • 匹配任何以E元素开头的F元素。

    E~F {     适当的价值; }


使用Javascript

    // JavaScript code:
    document.getElementsByClassName("childDiv")[0].parentNode;

    // jQuery code:
    $('.childDiv').parent().get(0); // This would be the childDiv's parent <div>.