CSS3选择器:选择特定节点的父节点? [英] CSS3 Selector: select a parent node of a specific node?

查看:2166
本文介绍了CSS3选择器:选择特定节点的父节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

是否有CSS父选择器?


Hi !



我试图选择一个特定节点(具有特定className)的父节点来应用一些CSS样式。



据我所知,对于子元素,后代,后续节点等,只存在CSS3选择器操作数...因此,只有一些在DOM文档中的转发选择是可能的。当选择器应用于DOM文档中的某个部分时,始终选择选择器描述的最后一个元素。我错了吗?



如何在下面的示例中选择第一个< div> 元素?让我们说,可能存在很多其他包含< p> 的其他< div> 要选择包含 p.foo 但不包含 p的< div> bar 。请注意,我要选择< div> ,而不是< p> ! b
$ b

 < div> 
< h1>测试< / h1>
< p class =foo>一些文字< / p>
< / div>
< div>
< h1>测试2< / h1>
< p class =bar>一些其他文字< / p>
< / div>

提前感谢!

方案

确实,父选择器不存在。

这里可以看到选择器列表:
http://www.w3.org/TR/css3-selectors/#selectors



您可以为父节点指定一个id,然后选择其父标识符。

否则,我没有看到任何解决方案只使用CSS从底部向下访问div。 / p>

Possible Duplicate:
Is there a CSS parent selector?

Hi!

I'm trying to select a parent node of a specific node (with a specific className) to apply some CSS style to it.

As far as I know, there only exist CSS3 selector operands for child elements, descendant, following nodes etc... So only some "forward" selection in the DOM document is possible. When the selector applies to some section in the DOM document, always the last element the selector describes, is being selected. Am I wrong? I hope so!

How do you select the first <div> element in the following example? Let's say that there may exist a lot of other <div>s containing <p>s and I only want to select the <div>s containing a p.foo but not p.bar. Note that I want to select the <div> rather than the <p>!

<div>
    <h1>Test</h1>
    <p class="foo">Some text</p>
</div>
<div>
    <h1>Test 2</h1>
    <p class="bar">Some other text</p>
</div>

Thanks in advance!

解决方案

Indeed a "parent selector" doesn't exist.
You can see the list of selectors here: http://www.w3.org/TR/css3-selectors/#selectors

You could give your parent node an id and then select the parent with its id.
Otherwise I don't see any solution to access the div from bottom up using solely CSS.

这篇关于CSS3选择器:选择特定节点的父节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆