开发者

CSS selector for "foo that contains bar"? [duplicate]

开发者 https://www.devze.com 2022-12-15 21:52 出处:网络
This question already has answers here: Is there a CSS parent selector? (33 answers) Closed 2 years ago.
This question already has answers here: Is there a CSS parent selector? (33 answers) Closed 2 years ago.

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

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.

(I'm aware I could pull this off with jQuery - $("obj开发者_高级运维ect param").closest("object") - and VanillaJS - document.querySelector("object param").closest("object") - but I'm trying to create CSS rules on a page.)


To select all OBJECT containing PARAM, in CSS:

OBJECT:has(PARAM)

To select all OBJECT having a direct child PARAM, in CSS:

OBJECT:has(> PARAM)

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.

Here are some similar questions with similar results:

  • Is there a CSS parent selector?
  • CSS Parent/Ancestor Selector
  • Complex CSS selector for parent of active child


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

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

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


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

<object class="hasparams">

then do

object.hasparams
0

精彩评论

暂无评论...
验证码 换一张
取 消