这可能看起来很愚蠢..我想给p标签赋予样式,该标签位于某个div之后(div不是p标签的父节点).例如
<div>Hai</div> <p>Hello</p> <p>How are you?</p>
在这里,我想给p标签赋予样式.我不能把这些p标签放在另一个div中.我想给所有的p标签后跟div相同的样式.而且我也不能普遍给p标签的样式,因为它会影响其他页面.有人可以帮忙吗?希望我对我的问题很清楚……
解决方法
你必须使用一般兄弟选择器:
div ~ p { color: red; }
看到这个jsFiddle:http://jsfiddle.net/yvdN6/4/.
你可以看到doc:
The elements don’t have to be adjacent siblings,but they have to have the same parent,and the
div
element has to occur before thep
element in the document source.