Using :focus to style outer div?

后端 未结 10 685
礼貌的吻别
礼貌的吻别 2020-12-02 18:24

When I begin writing text in the textarea, I want the outer div, with a class box, to have it\'s border turned solid instead of dashed, but somehow the :focus doesn\'t apply

相关标签:
10条回答
  • 2020-12-02 18:28

    This can now be achieve through the css method :focus-within as examplified in this post: 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;
    }
    <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>

    0 讨论(0)
  • 2020-12-02 18:30

    focus-within

    .box:focus-within {
      background: cyan;
    }
    

    read more here

    0 讨论(0)
  • 2020-12-02 18:33

    As far as I am aware you have to use javascript to travel up the dom.

    Something like this:

    $("textarea:focus").parent().attr("border", "thin solid black");
    

    you'll need the jQuery libraries loaded as well.

    0 讨论(0)
  • 2020-12-02 18:34

    Other posters have already explained why the :focus pseudo class is insufficient, but finally there is a CSS-based standard solution.

    CSS Selectors Level 4 defines a new pseudo class:

    :focus-within

    From MDN:

    The :focus-within CSS pseudo-class matches any element that the :focus pseudo-class matches or that has a descendant that the :focus pseudo-class matches. (This includes descendants in shadow trees.)

    So now with the :focus-within pseudo class - styling the outer div when the textarea gets clicked becomes trivial.

    .box:focus-within {
        border: thin solid black;
    }
    

    .box {
        width: 300px;
        height: 300px;
        border: 5px dashed red;
    }
    
    .box:focus-within {
        border: 5px solid green;
    }
    <p>The outer box border changes when the textarea gets focus.</p>
    <div class="box">
        <textarea rows="10" cols="25"></textarea>
    </div>

    Codepen demo

    NB: Browser Support : Chrome (60+), Firefox and Safari

    0 讨论(0)
  • 2020-12-02 18:35

    Simple use JQuery.

    $(document).ready(function() {
      $("div .FormRow").focusin(function() {
        $(this).css("background-color", "#FFFFCC");
        $(this).css("border", "3px solid #555");
      });
      $("div .FormRow").focusout(function() {
        $(this).css("background-color", "#FFFFFF");
        $(this).css("border", "0px solid #555");
      });
    });
        .FormRow {
          padding: 10px;
        }
    <html>
    
    <head>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    </head>
    
    <body>
      <div style="border: 0px solid black;padding:10px;">
        <div class="FormRow">
          First Name:
          <input type="text">
          <br>
        </div>
        <div class="FormRow">
          Last Name:
          <input type="text">
        </div>
      </div>
    
      <ul>
        <li><strong><em>Click an input field to get focus.</em></strong>
        </li>
        <li><strong><em>Click outside an input field to lose focus.</em></strong>
        </li>
      </ul>
    </body>
    
    </html>

    0 讨论(0)
  • 2020-12-02 18:38

    You can tab between div tags. Just add a tab index to the div. It's best to use jQuery and CSS classes to solve this problem. Here's a working sample tested in IE, Firefox, and Chrome (Latest versions... didn't test older).

    <html>
        <head>
            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
            <script type="text/javascript">
                var divParentIdFocus;
                var divParentIdUnfocus = null;
    
                $(document).ready(function() {              
    
                        $("div").focus(function() {
                            //$(this).attr("class", "highlight");
                            var curDivId = $(this).attr("id");
    
                            // This Check needs to be performed when/if div regains focus
                            // from child element.
                            if (divParentIdFocus != curDivId){
                                divParentIdUnfocus = divParentIdFocus;
                                divParentIdFocus = curDivId;
                                refreshHighlight();
                            }
    
                            divParentIdFocus = curDivId;
                        });
    
    
                        $("textarea").focus(function(){
                            var curDivId = $(this).closest("div").attr("id");
    
                            if(divParentIdFocus != curDivId){
                                divParentIdUnfocus = divParentIdFocus;
                                divParentIdFocus = curDivId;
                                refreshHighlight();
                            }
                        });
    
                        $("#div1").focus();
                });
    
                function refreshHighlight()
                {
                    if(divParentIdUnfocus != null){
                        $("#" +divParentIdUnfocus).attr("class", "noHighlight");
                        divParentIdUnfocus = null;
                    }
    
                    $("#" + divParentIdFocus).attr("class", "highlight");
                }
            </script>
            <style type="text/css">
                .highlight{
                    background-color:blue;
                    border: 3px solid black;
                    font-weight:bold;
                    color: white;
                }
                .noHighlight{           
                }
                div, h1,textarea, select { outline: none; }
    
            </style>
        <head>
        <body>
            <div id = "div1" tabindex="100">
                <h1>Div 1</h1> <br />
                <textarea rows="2" cols="25" tabindex="101">~Your Text Here~</textarea> <br />
                <textarea rows="2" cols="25" tabindex="102">~Your Text Here~</textarea> <br />
                <textarea rows="2" cols="25" tabindex="103">~Your Text Here~</textarea> <br />
                <textarea rows="2" cols="25" tabindex="104">~Your Text Here~</textarea> <br />
            </div>
            <div id = "div2" tabindex="200">
                <h1>Div 2</h1> <br />
                <textarea rows="2" cols="25" tabindex="201">~Your Text Here~</textarea> <br />
                <textarea rows="2" cols="25" tabindex="202">~Your Text Here~</textarea> <br />
                <textarea rows="2" cols="25" tabindex="203">~Your Text Here~</textarea> <br />
                <textarea rows="2" cols="25" tabindex="204">~Your Text Here~</textarea> <br />
            </div>
            <div id = "div3" tabindex="300">
                <h1>Div 3</h1> <br />
                <textarea rows="2" cols="25" tabindex="301">~Your Text Here~</textarea> <br />
                <textarea rows="2" cols="25" tabindex="302">~Your Text Here~</textarea> <br />
                <textarea rows="2" cols="25" tabindex="303">~Your Text Here~</textarea> <br />
                <textarea rows="2" cols="25" tabindex="304">~Your Text Here~</textarea> <br />
            </div>
            <div id = "div4" tabindex="400">
                <h1>Div 4</h1> <br />
                <textarea rows="2" cols="25" tabindex="401">~Your Text Here~</textarea> <br />
                <textarea rows="2" cols="25" tabindex="402">~Your Text Here~</textarea> <br />
                <textarea rows="2" cols="25" tabindex="403">~Your Text Here~</textarea> <br />
                <textarea rows="2" cols="25" tabindex="404">~Your Text Here~</textarea> <br />
            </div>      
        </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题