“包含 bar 的 foo”的 CSS 选择器?

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

CSS selector for "foo that contains bar"?

csscss-selectors

提问by Michael Gundlach

Is there a way to make a CSS Selector that matches the following?

有没有办法制作与以下内容匹配的 CSS 选择器?

All OBJECT elements
  which have a PARAM element inside of them

The selector

选择器

OBJECT PARAM

doesn't work, as it matches the PARAM, not the OBJECT. I'd like to apply { display:none } to the objects; it's useless to apply that to the PARAMs.

不起作用,因为它匹配 PARAM,而不是 OBJECT。我想将 { display:none } 应用于对象;将其应用于 PARAM 是没有用的。

(I'm aware I could pull this off with jQuery - $("object param").closest("object")- and VanillaJS - document.querySelector("object param").closest("object")- but I'm trying to create CSS rules on a page.)

(我知道我可以使用 jQuery - $("object param").closest("object")- 和 VanillaJS - document.querySelector("object param").closest("object")- 但我正在尝试在页面上创建 CSS 规则。)

采纳答案by Michael Greene

No, what you are looking for would be called a parent selector. CSS has none; they have been proposed multiple times but I know of no existing or forthcoming standard including them. You are correct that you would need to use something like jQuery or use additional class annotations to achieve the effect you want.

不,您正在寻找的将称为父选择器。CSS 没有;它们已被多次提出,但我知道没有包含它们的现有或即将推出的标准。您是正确的,您需要使用 jQuery 之类的东西或使用其他类注释来实现您想要的效果。

Here are some similar questions with similar results:

以下是一些具有相似结果的类似问题:

回答by Tatu Ulmanen

Only thing that comes even close is the :containspseudo class in CSS3, but that only selects textual content, not tags or elements, so you're out of luck.

唯一接近的是:containsCSS3 中的伪类,但它只选择文本内容,而不是标签或元素,所以你很不走运。

A simpler way to select a parent with specific children in jQuery can be written as (with :has()):

在 jQuery 中选择具有特定子级的父级的更简单方法可以写为 (with :has()):

$('#parent:has(#child)');

回答by Jeepstone

Is there any way you could programatically apply a class to the object?

有什么方法可以以编程方式将类应用于对象?

<object class="hasparams">

then do

然后做

object.hasparams