jquery: div hide/show on focus issue

后端 未结 2 1736
眼角桃花
眼角桃花 2021-01-26 12:55

Thanks to inti\'s code I was able to figure out the answer!

jQuery:

$(document).ready(function(){
    $(\"body\").click(function(e) {
          


        
相关标签:
2条回答
  • 2021-01-26 13:19

    I think you r looking something like this...

    HTML:

    <a href="#">Click Me</a>
    
    <div id="login">
        <label>Username:</label><input type="text"/><br/>
        <label>Password:</label><input type="password"/><br/>
    </div>
    

    jQuery:

    $(document).ready(function(){
       $("#login").hide();
        $('a').bind('click', function() {
            $("#login").toggle().focus();
        });
        $("input").focusout(function(){
            $("#login").hide();
        });
    });
    

    http://jsfiddle.net/anish/ccFt9/1/

    0 讨论(0)
  • 2021-01-26 13:31

    The event you are looking for is .blur() not .focusout().

    Edit: click out of #mydiv to hide it:

    $("body").click(function(e) {
        if ($(e.target).closest("#mydiv").size() == 0) {
            $("#mydiv").hide();
        }
    });
    

    This hides #mydiv if what you clicked doesn't have it as one of it's parents.

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