本文介绍了jQuery:div隐藏/显示焦点问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助 inti的代码,我能够找出答案!

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

jQuery:

$(document).ready(function(){
    $("body").click(function(e) {
        //checks to see if the tag you clicked has the #login
        //div as a parent.
        if($(e.target).closest("#login").size() == 0) {
            //makes exception for the #login's <a> tag (id of a_login)
            if((e.target.id) == 'a_login') {
                $("#login").show();
            } else {
                $("#login").hide();
            }
        //if target has #login as parent, keep it visible
        } else {
            $("#login").show();
        }
    });

});

有效的jsfiddle示例: http://jsfiddle.net/SuperPhil/3CmE7/

Working jsfiddle example: http://jsfiddle.net/SuperPhil/3CmE7/

谢谢!

我正在尝试使用类似于 http://dropbox.com 登录的登录屏幕.用户单击登录链接的位置会出现一个div(CSS),而当焦点移开时,就会消失.

I am trying to have a login screen similar to the http://dropbox.com login. Where a user clicks on the login link and a div appears (CSS) and when focus out, disappears.

这是我的jQuery

Here is my jQuery

$('#login').bind('click', function() {
    $('#login').addClass("clicked").focusout(function() {
        $('#login').removeClass('clicked');
    });
});

这不起作用.有人可以帮我吗?

This doesn't work. Can anyone help me out?

$('#login').click(function() {
    $('#login div').addClass("clicked").blur(function() {
        $('#login div').removeClass('clicked');
    });

});

推荐答案

您要查找的事件是.blur()而不是.focusout().

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

编辑:在#mydiv中单击以将其隐藏:

click out of #mydiv to hide it:

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

如果您单击的内容没有它作为父项之一,则会隐藏#mydiv.

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

这篇关于jQuery:div隐藏/显示焦点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 04:55
查看更多