开发者

Does CSS have anything like jQuery's :has()?

开发者 https://www.devze.com 2023-03-11 16:52 出处:网络
In CSS (any version), is there something like, or any other way of doing anything like the :has() selector in jQuery?

In CSS (any version), is there something like, or any other way of doing anything like the :has() selector in jQuery?

jQuery(':has(selector)')

Description: Selects elements which contain at least one element that matches the specified selector.

http://api.jquery开发者_运维问答.com/has-selector/


No, there isn't. The way CSS is designed, does not permit selectors that match ancestors or preceding siblings; only descendants ( and >), succeeding siblings (~ and +) or specific children (:*-child). The only ancestor selector is the :root pseudo-class which selects the root element of a document (in HTML pages, naturally it would be html).

If you need to apply styles to the element you're querying with :has(), you need to add a CSS class to it then style by that class, as suggested by Stargazer712.


No. The best way to accomplish this is by using jQuery:

Css File:

.myAwesomeClass {
    ...
}

Js File:

jQuery(':has(selector)').addClass("myAwesomeClass")

where selector is whatever it is you were originally trying to match.

0

精彩评论

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