我可以仅使用CSS来检测元素可见性吗? [英] Can I detect element visibility using only CSS?
问题描述
我检查了API是否有伪选择器,例如:visible
或:hidden
,但对找不到这样的选择器。由于jQuery已经支持这些选择器已有一段时间了,所以我希望可以实现它们。这个想法是,我只想显示某个元素旁边的元素,但我不想使用JavaScript来显示它。
I checked the API for some pseudo-selector such as :visible
or :hidden
, but was disappointed to find no such selector exists. Since jQuery has supported these selectors for a while now, I was hoping they'd be implemented. The idea is, that I'd like to show a certain element only when the element next to it is hidden, but I don't want to use JavaScript to do so. Any options?
推荐答案
否,这是不可能的,不可能的,至少
No, it is not possible and can't be possible, at least in stylesheets.
否则,您可以创建一个无限循环:
Otherwise, you could create an infinite loop:
element:visible {
display: none;
}
元素将首先显示,然后选择器将其选中并隐藏
The element would be visible at first, then the selector would select it and hide it, then the selector wouldn't apply and it would become visible again, etc.
只有在像<$这样的JS API中才允许使用该伪类。 c $ c> querySelector 。但是据我所知,没有类似的东西,而且也不只是CSS。
It would be possible to allow that pseudo-class only in JS APIs like querySelector
. But as far as I know there isn't anything like that, and it wouldn't be CSS-only.
这篇关于我可以仅使用CSS来检测元素可见性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!