Using jQuery to test if an input has focus

后端 未结 15 1145
梦谈多话
梦谈多话 2020-11-22 05:42

On the front page of a site I am building, several

s use the CSS :hover pseudo-class to add a border when the mouse is over them. One of
相关标签:
15条回答
  • 2020-11-22 06:17

    I'm not entirely sure what you're after but this sounds like it can be achieved by storing the state of the input elements (or the div?) as a variable:

    $('div').each(function(){
    
        var childInputHasFocus = false;
    
        $(this).hover(function(){
            if (childInputHasFocus) {
                // do something
            } else { }
        }, function() {
            if (childInputHasFocus) {
                // do something
            } else { }
        });
    
        $('input', this)
            .focus(function(){
                childInputHasFocus = true;
            })
            .blur(function(){
                childInputHasFocus = false;
            });
    });
    
    0 讨论(0)
  • 2020-11-22 06:19

    An alternative to using classes to mark the state of an element is the internal data store functionality.

    P.S.: You are able to store booleans and whatever you desire using the data() function. It's not just about strings :)

    $("...").mouseover(function ()
    {
        // store state on element
    }).mouseout(function ()
    {
        // remove stored state on element
    });
    

    And then it's just a matter of accessing the state of elements.

    0 讨论(0)
  • 2020-11-22 06:20

    Simple

     <input type="text" /> 
    
    
    
     <script>
         $("input").focusin(function() {
    
        alert("I am in Focus");
    
         });
     </script>
    
    0 讨论(0)
  • 2020-11-22 06:24

    CSS:

    .focus {
        border-color:red;
    }
    

    JQuery:

      $(document).ready(function() {
    
        $('input').blur(function() {
            $('input').removeClass("focus");
          })
          .focus(function() {
            $(this).addClass("focus")
          });
      });
    
    0 讨论(0)
  • 2020-11-22 06:24

    As far as I know, you can't ask the browser if any input on the screen has focus, you have to set up some sort of focus tracking.

    I usually have a variable called "noFocus" and set it to true. Then I add a focus event to all inputs that makes noFocus false. Then I add a blur event to all inputs that set noFocus back to true.

    I have a MooTools class that handles this quite easily, I'm sure you could create a jquery plugin to do the same.

    Once that's created, you could do check noFocus before doing any border swapping.

    0 讨论(0)
  • 2020-11-22 06:24

    What I wound up doing is creating an arbitrary class called .elementhasfocus which is added and removed within the jQuery focus() function. When the hover() function runs on mouse out, it checks for .elementhasfocus:

    if(!$("#quotebox").is(".boxhasfocus")) $(this).removeClass("box_border");
    

    So if it doesn't have that class (read: no elements within the div have focus) the border is removed. Otherwise, nothing happens.

    0 讨论(0)
提交回复
热议问题