使用:专注于外部div的风格?

Web*_*urk 57 css focus stylesheet

当我开始在textarea中编写文本时,我希望带有类框的外部div使其边框变为实线而不是虚线,但不知何故:在这种情况下焦点不适用.如果它适用于:活动,它怎么会不起作用:焦点?

有什么想法吗?

(注意.我希望DIV的边框变得坚固,而不是textareas)

div.box
{
    width: 300px;
    height: 300px;
    border: thin dashed black;
}

div.box:focus{
    border: thin solid black;
}

<div class="box">
    <textarea rows="10" cols="25"></textarea>
</div>
Run Code Online (Sandbox Code Playgroud)

Kar*_*yan 65

DIV如果设置tabindex属性,元素可以获得焦点.这是工作示例.

#focus-example > .extra {
  display: none;
}
#focus-example:focus > .extra {
  display: block;
}
Run Code Online (Sandbox Code Playgroud)
<div id="focus-example" tabindex="0">
  <div>Focus me!</div>
  <div class="extra">Hooray!</div>
</div>
Run Code Online (Sandbox Code Playgroud)

有关详细信息focus,并blur,你可以看看这篇文章.

更新: 这是另一个focus用于创建的示例menu.

#toggleMenu:focus {
  outline: none;
}
button:focus + .menu {
  display: block;
}
.menu {
  display: none;
}
.menu:focus {
  display: none;
}
Run Code Online (Sandbox Code Playgroud)
<div id="toggleMenu" tabindex="0">
  <button type="button">Menu</button>
  <ul class="menu" tabindex="1">
    <li>Home</li>
    <li>About Me</li>
    <li>Contacts</li>
  </ul>
</div>
Run Code Online (Sandbox Code Playgroud)


Dan*_*eld 53

其他海报已经解释了为什么:focus伪类不够,但最后有一个基于CSS的标准解决方案.

CSS Selectors Level 4定义了一个新的伪类:

:焦点之内

来自MDN:

:focus-withinCSS伪类匹配任何元素的:focus 伪类匹配或具有后代的:focus 伪类比赛.(这包括阴影树中的后代.)

所以现在使用:focus-within伪类 - 当textarea被点击时,外部div的样式变得微不足道.

.box:focus-within {
    border: thin solid black;
}
Run Code Online (Sandbox Code Playgroud)

.box {
    width: 300px;
    height: 300px;
    border: 5px dashed red;
}

.box:focus-within {
    border: 5px solid green;
}
Run Code Online (Sandbox Code Playgroud)
<p>The outer box border changes when the textarea gets focus.</p>
<div class="box">
    <textarea rows="10" cols="25"></textarea>
</div>
Run Code Online (Sandbox Code Playgroud)

Codepen演示

NB: 浏览器支持:Chrome(60+),Firefox和Safari

  • 哇,没想到这个存在,这是一个非常有用的选择器修饰符! (2认同)
  • 惊人的答案:) (2认同)
  • 到目前为止,最简单和最好的答案。也不知道这个选择器存在。根据我的测试在 Mozilla 和 Chrome 中工作。 (2认同)

Dav*_*mas 35

虽然单靠CSS/HTML无法实现这一点,但可以通过JavaScript实现(不需要库):

var textareas = document.getElementsByTagName('textarea');

for (i=0;i<textareas.length;i++){
    // you can omit the 'if' if you want to style the parent node regardless of its
    // element type
    if (textareas[i].parentNode.tagName.toString().toLowerCase() == 'div') {
        textareas[i].onfocus = function(){
            this.parentNode.style.borderStyle = 'solid';
        }
        textareas[i].onblur = function(){
            this.parentNode.style.borderStyle = 'dashed';
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

JS小提琴演示.

顺便说一下,使用jQuery这样的库,上面的内容可以简化为:

$('textarea').focus(
    function(){
        $(this).parent('div').css('border-style','solid');
    }).blur(
    function(){
        $(this).parent('div').css('border-style','dashed');
    });
Run Code Online (Sandbox Code Playgroud)

JS小提琴演示.

参考文献:

  • 不再是引入的最有效的答案:focus-within伪元素! (7认同)
  • +1的详细答案(尤其是不需要jQuery的答案) (4认同)

小智 6

聚焦于内部

.box:focus-within {
  background: cyan;
}
Run Code Online (Sandbox Code Playgroud)

在这里阅读更多内容


Tek*_*ill 5

现在,可以通过本文:focus-within中举例说明的css方法来实现:http : //www.scottohara.me/blog/2017/05/14/focus-within.html

/*
  A normal (though ugly) focus
  pseudo-class.  Any element that
  can receive focus within the
  .my-element parent will receive
  a yellow background.
*/
.my-element *:focus {
  background: yellow !important;
  color: #000;
}

/*
  The :focus-within pseudo-class
  will NOT style the elements within
  the .my-element selector, like the
  normal :focus above, but will
  style the .my-element container
  when its focusable children
  receive focus.
*/
.my-element:focus-within {
  outline: 3px solid #333;
}
Run Code Online (Sandbox Code Playgroud)
<div class="my-element">
  <p>A paragraph</p>
  <p>
    <a href="http://scottohara.me">
      My Website
    </a>
  </p>

  <label for="wut_email">
    Your email:
  </label>
  <input type="email" id="wut_email" />
</div>
Run Code Online (Sandbox Code Playgroud)