2017-08-30 90 views

回答

1

这是可能的使用:focus-within伪类选择。

焦点在CSS伪类匹配:焦点伪类匹配或具有:焦点伪类匹配后代的任何元素。 (这包括在阴影栽树后人。)

:focus-within @ MDN

Support Details

lable { 
 
    margin: 1em; 
 
    border: 1px solid grey; 
 
} 
 

 
label:focus-within p { 
 
    color: red; 
 
}
<label> 
 
    <p>text description</p> 
 
    <input type="text"> 
 
</label>

+0

您可以检查p标签的颜色没有改变 – Durga

+0

它适用于Chrome 60和FF –

6

您不能从<input>访问前一个同级元素(<p>)。

但是你可以采用如下方案:

label { 
 
    display:flex; 
 
} 
 
p { 
 
    order:-1; 
 
} 
 
input:focus ~ p { 
 
    color:red; 
 
}
<label> 
 
    <input type="text"> 
 
    <p>text description</p> 
 
</label>

相关问题