且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在输入焦点上更改标签的颜色.如何?

更新时间:2023-12-06 12:54:16

我认为不更改HTML就可以,请参见:

I don't think you can without changing your HTML, see also: Is there any way to hover over one element and affect a different element?, your elements should be direct siblings. (LESS don't help to solve your problem here, LESS generate CSS and it seems impossible to do in CSS)

可能的建议:

input:focus + .control-label
{
    background-color:purple;
    color: red;
}
.controls > input
{
    float:right;
}	

<div class="controls">
    <input type="text" id="inputEmail" placeholder="Firstname">
    <label class="control-label" for="inputEmail">Firstname</label>
</div>

或使用javascript解决您的问题: https://***.com/a/20226218/1596547

Or solve your problem with javascript: https://***.com/a/20226218/1596547