将样式应用于包含某个类div的div [英] Apply style to a div that contains a div of some class
问题描述
在这个例子中是否可以将样式应用于外部div,仅使用CSS以及存在类错误的内部div的事实?
Is it possible to apply a style to the outer div in this example, using only CSS and the fact that there is an inner div of class "error"?
<div>
<div class = "error">
</div>
</div>
我知道我可以在javascript中完成。
I know I could do it in javascript.
编辑
由于纯css不可能,这样做的效率真的很低吗?有更干净的方式吗?我不能轻易改变dom。对于后台,错误类会自动包裹在rails中的标签/输入中,但我想设置包含DIV的样式。
Since it's not possible in pure css, is it really inefficient to do something like this? Is there a cleaner way? I can't easily change the dom. For background, the error classes are automatically wrapped around labels/inputs in rails but I want to style the containing DIVs.
$(".error").parent().addClass("error-wrapper");
推荐答案
仅使用CSS是不可能的。
无法在DOM树中找到父元素。
It is not possible using only CSS. There is no way to find an elements parent in the DOM tree.
但是很容易使用像jQuery这样的东西。
But is pretty easy to use something like jQuery.
$(".error").parent().addClass("example");
这篇关于将样式应用于包含某个类div的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!