聚焦时执行操作

use*_*330 1 html css

我有3个div.其中2个在聚焦时会改变颜色.当其中两个div聚焦时,还可以对另一个div执行操作吗?

div {
  border: 1px solid;
  margin: 5px;
  width: 300px;
  height: 50px;
  padding: 2px;
}
.myClass:focus {
  background-color: yellow;
  outline: none;
}
Run Code Online (Sandbox Code Playgroud)
<div class="myClass" tabindex="-1">
  Focus me!
</div>
<div class="myClass" tabindex="-1">
  You can focus me too!
</div>
<hr />
<div class="anotherClass">
  I cannot be focused, but want to change my color, when one of the other divs above me get focused.
</div>
Run Code Online (Sandbox Code Playgroud)

因此,当2个上部div中的1个聚焦时,我希望底部的第3个div改变其颜色.

在这里你可以看看:https://jsfiddle.net/ogpvvwtg/

j08*_*691 6

当然,您可以使用通用兄弟选择器~

.myClass:focus ~ .anotherClass {
    background-color: red;
    outline: none;
}
Run Code Online (Sandbox Code Playgroud)

div {
    border: 1px solid;
    margin: 5px;
    width: 300px;
    height: 50px;
    padding: 2px;
  }
  .myClass:focus {
    background-color: yellow;
    outline: none;
  }
  .myClass:focus ~ .anotherClass {
    background-color: red;
    outline: none;
  }
Run Code Online (Sandbox Code Playgroud)
<div class="myClass" tabindex="-1">
  Focus me!
</div>
<div class="myClass" tabindex="-1">
  You can focus me too!
</div>
<hr />
<div class="anotherClass">
  I cannot be focused, but want to change my color, when one of the other divs above me get focused.
</div>
Run Code Online (Sandbox Code Playgroud)