在div外部单击时鼠标功能

时间:2013-08-18 15:08:14

标签: javascript jquery html mouseup

我正在尝试实现一个mouseup函数,如果我点击容器外的任何地方,就会出现一个函数。见下面的脚本。

该功能运行良好,但如果我点击页面上的任何位置,就会发生这种情况。

我正在尝试创建一个'if'条件,如果鼠标单击在函数中涉及的容器或其任何后代中,则不会发生mouseup函数。

有谁可以告诉我它为什么不能正常工作?非常感谢..

剧本:

$(document).mouseup(function (e)
{
var container = $('#containerprA');
var containerSW = $('#containerSW');


if (!container.is(e.target) // if the target of the click isn't the container...
    && container.has(e.target).length === 0); // ... nor a descendant of the container

if (!containerSW.is(e.target) // if the target of the click isn't the container...
    && containerSW.has(e.target).length === 0) // ... nor a descendant of the container

{
        container.fadeOut('slow',function(){
        containerSW.fadeIn('slow');
    });
}
});

1 个答案:

答案 0 :(得分:0)

<强> Demo 试试这个

$(document).ready(function(){
$('#containerSW').hide();
$(document).on('mouseup', function(e) {
    if (!$(e.target).is('#containerprA') && !$(e.target).parents().is('#containerprA')) {

        $('#containerprA').fadeOut("slow");
        $('#containerSW').fadeIn('slow');
    }
});
});

希望这有帮助,谢谢

相关问题