onClickOut (click after leaving the element)

This question already has an answer here:

  • How do I detect a click outside an element? 76 answers

  • You do that by listening to a click on the document level, and inside the event handler you check if the clicked element was #schnellsuche_box or any element inside #schnellsuche_box by using closest() , like so :

    $(document).on('click', function(e) {
        if ( ! $(e.target).closest('#schnellsuche_box').length ) 
             $('#schnellsuche_box').hide();
    });
    

    FIDDLE


    您需要通过return false来停止#schnellsuche_box点击事件冒泡到body单击事件(这是默认事件传播):

    $("#schnellsuche_box").click(function() {
        inside = true;
    
        return false;
    });
    

    尝试这个:

    $("body").click(function(e) {
       var $target = $(e.target);
       if (!$target.is('#schnellsuche_box') &&
           !$target.parents('#schnellsuche_box').length) {
           alert('outside');
       }
    });
    
    链接地址: http://www.djcxy.com/p/14702.html

    上一篇: 如何检测外部的CSS模式点击?

    下一篇: onClickOut(离开元素后点击)