开发者

Is there any valid reason there're no parent selectors in CSS?

开发者 https://www.devze.com 2023-03-06 07:35 出处:网络
While there exist selec开发者_运维技巧tors to select items preceded (#hlinks+#hsearch) or owned (#topbar>#hlinks) by other items, there\'s no way to do the opposite.

While there exist selec开发者_运维技巧tors to select items preceded (#hlinks+#hsearch) or owned (#topbar>#hlinks) by other items, there's no way to do the opposite.

For example there isn't something like

li:has(ul){ }

To detect list items that have other lists within them. Wouldn't that be convenient?

AFAIK, the feature is not even in the plans for CSS, so my question is: why is this so?


This is generally called a "parent selector"; as you say, they don't exist in CSS, though they could be useful.

There's an interesting discussion here; the summary is that they would have a large negative effect on performance and would allow people to make mistakes with large consequences. People who understand these things seem to think that these issues can be overcome, but there hasn't yet been sufficient demand for someone to actually do it.


You're referring to the concept of a "parent" selector - selecting the parent element under a particular condition. You're correct about this feature not being present in CSS (not event CSS3) - to my knowledge, it not possible at the moment to the way the DOM is parsed by CSS. However, this feature is available using jQuery and the :parent selector or the :has selector, which it seems you may be familiar with already.

EDIT: For a tremendous amount of detail on the idea of a parent selector, see http://shauninman.com/archive/2008/05/05/css_qualified_selectors.


This is standard in CSS since...uh.... the beginning of CSS.

It's just a space:

li ul means any ul that is inside li


If you want to do that just do:

ul li ul li {

}

So now you are searching for any list-item that has an unordered-list inside of it with a list item.

0

精彩评论

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

关注公众号