【发布时间】:2020-05-20 17:27:17
【问题描述】:
是否可以使用 CSS/PostCSS (magic) 仅在目标输入聚焦且不为空时才显示后续元素?
当输入焦点集中时,我已经完成了一半的显示,但还不知道是否将焦点和空结合在一起?
input:not(:focus) + button {
opacity: 0;
pointer-events: none;
touch-action: none;
}
input:not(:focus):not(:empty) + .notEmptyCase {
opacity: 0;
pointer-events: none;
touch-action: none;
}
<p>Button gets visible when input is focused</p>
<input placeholder="Search">
<button>CLEAR</button>
<p>Button gets visible when input is focused and isn't empty</p>
<input placeholder="Search">
<button class='notEmptyCase'>CLEAR</button>
【问题讨论】:
-
我很确定这在 CSS 中是不可能的,因为
:empty本质上意味着该元素没有“子元素”,而input不能有子元素,因此它将始终适用- :empty CSS 伪类表示任何没有子元素的元素。