当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - popup not handled correctly

问题描述:

i m trying to make a popover for my project, it closes when i click else where in the document but the problem is that it also closes when i click on popover div. it should be closed when i click somewhere else in the document not on the div.

fiddle here

$(document).ready(function(){

jQuery.fx.speeds = {

fast: 120

};

$("#popup-img").click(function (e) {

if ($(".popup").is(":hidden")) {

$(".checkbox input").removeAttr("checked");

$(".popup").slideDown("fast");

}

else {

$(".popup").slideUp("fast");

}

e.stopPropagation();

});

$("body").click(function(){

$(".popup").slideUp("fast");

});

});

网友答案:

You need to test whether the click happened within the popup

$("body").click(function (e) {
    if ($(e.target).closest('.popup').length == 0) {
        $(".popup").slideUp("fast");
    }
});

Demo: Fiddle

网友答案:

try this

$(document).click(function(){
if (!$('.popup').is(e.target) && $('.popup').has(e.target).length === 0) {
      $(".popup").slideUp("fast");
 }

});

fiddle here

网友答案:

Try changing:

$("body").click(function(){
  $(".popup").slideUp("fast");
});

To this:

$(document).click(function(){
  $(".popup").slideUp("fast");
});

$(".popup").click(function(e){
  e.stopPropagation();
});
网友答案:

EDIT

my old fiddle didnt do the hole job, here is the new one that works:

http://jsfiddle.net/cuYBD/11/

In the fiddle i replaced the body with a div#mybody.

change your body.click Function to:

$("body").click(function(e){
    var bodyfound = false;
    $(e.target).parents().each(function() {
        if($(this).is(".popup")) {
            bodyfound=true;
           return;
    }
    });
    if(!bodyfound) {
        $(".popup").slideUp("fast");
    }
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: