开发者

How to select body except one element?

开发者 https://www.devze.com 2023-03-26 17:01 出处:网络
I\'m using a color-pic开发者_运维问答ker which should be hidden when a click is made anywhere outside it.

I'm using a color-pic开发者_运维问答ker which should be hidden when a click is made anywhere outside it. The problem is, it disappears even when the click is made inside the picker.

$('body :not(#picker)').click(function() {
    $('#picker').fadeOut();
});

I tried this, but it would show the picker and hide it immediately. Does anybody have a suggestion?


Try using event.target to obtain the element that was clicked:

$("body").click(function(event) {
    if (event.target.id != "picker") {
        $("#picker").fadeOut();
    }
});


$("body").click(function(e) {
    if ($(e.target).attr('id') == 'picker') {
        return;
    } else {
        $('#picker').fadeOut();
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消