Html 在另一个元素之后选择第一个出现的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4623328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select first occurring element after another element
提问by Rob
I've got the following HTML code on a page:
我在页面上有以下 HTML 代码:
<h4>Some text</h4>
<p>
Some more text!
</p>
In my .css
I've got the following selector to style the h4
element. The HTML code above is just a small part of the entire code; there are several div
s more wrapped around belonging to a shadowbox:
在我的.css
我有以下选择器来设置h4
元素的样式。上面的 HTML 代码只是整个代码的一小部分;还有几个div
属于shadowbox的包裹:
#sb-wrapper #sb-wrapper-inner #sb-body #myDiv h4
{
color : #614E43;
margin-top : 5px;
margin-left : 6px;
}
So, I have the correct style for my h4
element, but I also want to style the p
tag in my HTML.
所以,我的h4
元素有正确的样式,但我也想p
在我的 HTML 中设置标签的样式。
Is this possible with CSS-selectors? And if yes, how can I do this?
这可以用 CSS 选择器实现吗?如果是,我该怎么做?
回答by Phrogz
回答by Joel Mellon
For your literal example you'd want to use the adjacent selector (+).
对于您的文字示例,您需要使用相邻的选择器 (+)。
h4 + p {color:red}//any <p> that is immediately preceded by an <h4>
<h4>Some text</h4>
<p>I'm red</p>
<p>I'm not</p>
However, if you wanted to select all successive paragraphs, you'd need to use the general sibling selector (~).
但是,如果您想选择所有连续的段落,则需要使用通用同级选择器 (~)。
h4 ~ p {color:red}//any <p> that has the same parent as, and comes after an <h4>
<h4>Some text</h4>
<p>I'm red</p>
<p>I am too</p>
回答by Tom
Just hit on this when trying to solve this type of thing my self.
在我自己尝试解决此类问题时,只需点击此按钮即可。
I did a selector that deals with the element after being something other than a p.
我做了一个选择器,它在不是 p 之后处理元素。
.here .is.the #selector h4 + * {...}
Hope this helps anyone who finds it :)
希望这可以帮助任何找到它的人:)
回答by Trooller
Simplyfing for the begginers:
简单的初学者:
If you want to select an element immediatly after another element you use the +
selector.
如果您想在另一个元素之后立即选择一个元素,请使用+
选择器。
For example:
例如:
div + p
The "+" element selects all <p>
elements that are placed immediately after <div>
elements
div + p
“+”元素选择<p>
紧跟在<div>
元素之后的所有元素
If you want to learn more about selectors use this table
如果您想了解有关选择器的更多信息,请使用此表