简体   繁体   中英

Cannot lose focus when click outside input

So the problem is every time I click outside the input focus keeps on i tried several codes but they didn't work for me

 $(document).ready(function(){ $("input").focus(function(){ const background = true; if(background == true){ $('form').css({ 'background-color': '#fff', 'transition': 'background-color 1s ease-out' }); } else { $('input').blur(function(){ if(background == false){ $('form').css({ 'background-color': 'transparent' }); } }) } })});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form class="form-inline"> <div class="input-group"> <input type="text" class="form-control input" placeholder="How may we help you?"> <div class="input-append"> <button type="button" class="btn"><i class="fas fa-search"></i></button> </div> </div> </form>

I think you wanna do something like this

  1. On Focus- Change color of form background to #fff(White)
  2. On unfocus - Change form background to transparent
$('input').focus(function () {
    $('form').css({
        'background-color': '#fff',
        'transition': 'background-color 1s ease-out'
    });
});

$('input').blur(function () {
    $('form').css({
        'background-color': 'transparent'
    });
});
$(document).ready(function(){
   $("input").focus(function(){
       $('form').css({
          'background-color': '#fff',
          'transition': 'background-color 1s ease-out'
           });
    });

    $('input').blur(function(){
        $('form').css({
           'background-color': 'transparent'
            });
    });
});

Try this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM