jQuery focus/blur on form, not individual inputs

后端 未结 4 650
臣服心动
臣服心动 2020-12-18 05:46

How could this be achieved via jQuery?

When the user clicks on any input in the form, it is focussed. When a user clicks out of the form from any input it is blurred

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-18 06:19

    You could do something like this:

    var check;
    
    $('form input').focus(function()
       {
        clearInterval(check); // remove setinterval if any
        // do something because the form is being used
       });
    
    $('form input').blur(function()
    {
        check = setInterval(function()
        {
            // do something because the form is not being used
        },500); // half-second delay
    });
    

    It's not the sexiest solution, but checks for if no focus in your form occurs after .5 seconds. Hope this helps.

提交回复
热议问题