CSS 一个标签的选择器后跟另一个标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1132366/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 20:49:03  来源:igfitidea点击:

Selector for one tag directly followed by another tag

csscss-selectors

提问by Mostafa Farghaly

This selects all <B>tags directly precededby <A>tags:

这将选择所有的<B>直接标签之前<A>标签:

A+B {
    /* styling */
}

What is the selector for all <A>tags directly followedby <B>tags?

所有<A>标签的选择器直接<B>标签之后是什么?

Here's sample HTML fitting my question:

这是适合我的问题的示例 HTML:

<a>some text</a>
<b>some text</b>

采纳答案by jeroen

You can't in css.

你不能在 css 中。

Edit:To be a bit more helpful, if you use for example jQuery (a JavaScript library), you can use .prev().

编辑:为了更有帮助,如果您使用例如 jQuery(一个 JavaScript 库),您可以使用.prev().

回答by Nick Presta

Do you mean to style A given that it has a B element directly inside or followed? Like this:

你的意思是给 A 设计风格,因为它直接在里面或后面有一个 B 元素?像这样:

<A>
    <B>
    </B>
</A>

// OR

<A>
</A>
<B>
</B>

You can't do such a thing in CSS (yet). Eric Meyer states that this kind of selector has been discussed quite a few times on the CSS mailing list, and isn't doable. Dave Hyatt, one of the core WebKit developers, comments with a good explanation of why it can't be done.

你不能在 CSS 中做这样的事情(还)。Eric Meyer 表示这种选择器已经在 CSS 邮件列表中讨论了很多次,但不可行。Dave Hyatt 是 WebKit 的核心开发人员之一,他很好地解释了为什么不能这样做。

Check out: Shaun Inman's blog postand the comment by Eric Meyer.
David Hyatt weighs in, too.

查看:Shaun Inman 的博客文章Eric Meyer评论
大卫·凯悦 (David Hyatt)也很重要。

回答by Marco Marsala

You can ONLY do the converse: This selects all tags directly preceded by tags.

您只能执行相反的操作:这将选择直接以标签开头的所有标签。

This is logically equivalent to your request.

这在逻辑上等同于您的请求。

I often use this to style a row of many checkboxes with labels

我经常用它来设计一行带有标签的复选框

CSS:

CSS:

label+input {
    margin-left: 4px;
}

DOM:

DOM:

<input id="a" name="a" type="checkbox"/><label for="a">...</label>
<input id="b" name="b" type="checkbox"/><label for="b">...</label>
<input id="c" name="c" type="checkbox"/><label for="c">...</label>