显示和隐藏外部div,当输入框聚焦和失焦时,使用CSS。

huangapple go评论115阅读模式
英文:

show and hide outer div when input is focus and not focus with css

问题

.v-text-field input:focus-within > .class-target {
    display: block;
}
英文:

I want to control a div to hide and show only with css when the input is focused or not. But the problem is that the input I have is very deep in some element. How do I make the div show when the input is focused and the div hide when the input is not focused?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.v-text-field input:focus-within &gt; .class-target {
   display: block;
}

.class-target{
   display: none;
}

<!-- language: lang-html -->

&lt;div class=&quot;v-input custom-search input-small v-text-field&quot;&gt;
  &lt;div class=&quot;v-input__control&quot;&gt;
    &lt;div class=&quot;v-input__slot&quot;&gt;
      &lt;div class=&quot;v-text-field__slot&quot;&gt;
        &lt;input id=&quot;input-17&quot; type=&quot;text&quot; value=&quot;&quot;&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;v-text-field__details&quot;&gt;
      &lt;div class=&quot;v-messages theme--light&quot;&gt;
        &lt;div class=&quot;v-messages__wrapper&quot; /&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;class-target&quot;&gt;Target&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 0

> :focus-within CSS 伪类匹配一个元素,如果该元素或其任何后代元素都处于焦点状态。

只需从输入中删除这个类。MDN 文档链接

.v-text-field:focus-within + .class-target {
   display: block;
}

.class-target {
   display: none;
   width: 100px;
   height: 100px;
   border: 1px solid;
}
<div class="v-input custom-search input-small v-text-field">
  <div class="v-input__control">
    <div class="v-input__slot">
      <div class="v-text-field__slot">
        <input id="input-17" type="text" value="">
      </div>
    </div>
    <div class="v-text-field__details">
      <div class="v-messages theme--light">
        <div class="v-messages__wrapper">
        </div>
      </div>
    </div>
  </div>
</div>

<div class="class-target">目标</div>
英文:

I think you misunderstood the focus-within class
> :focus-within CSS pseudo-class matches an element if the element or any of its descendants are focused

Just remove the class from input. MDN DOC HERE
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.v-text-field:focus-within + .class-target {
   display: block;
}

.class-target{
   display: none;
   width:100px;
   height:100px;
   border:1px solid;
}

<!-- language: lang-html -->

&lt;div class=&quot;v-input custom-search input-small v-text-field&quot;&gt;
  &lt;div class=&quot;v-input__control&quot;&gt;
    &lt;div class=&quot;v-input__slot&quot;&gt;
      &lt;div class=&quot;v-text-field__slot&quot;&gt;
        &lt;input id=&quot;input-17&quot; type=&quot;text&quot; value=&quot;&quot;&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;v-text-field__details&quot;&gt;
      &lt;div class=&quot;v-messages theme--light&quot;&gt;
        &lt;div class=&quot;v-messages__wrapper&quot;&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;class-target&quot;&gt;Target&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月7日 12:21:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定