jQuery: Close DIV by clicking anywhere apart from the DIV itself?

后端 未结 3 1909

I have a div that slides down when a button is clicked, and I would like to slide up the div when a user:

  1. Clicks anwhere except within the DIV itself
3条回答
  •  星月不相逢
    2020-12-16 14:32

    i would do something like this

    var $doc, $panel = $('.panel-tab');
    $doc = $(document);
    $doc.bind("click", function(){
       $panel.hide();
       $doc.undbind("click");
    });
    
    $panel.bind("click", function(e){
       e.stopPropagation();
    });
    

    you always close the tab on the click on the document, but you stop the event from propagation on the tab it self.

    here a working example: http://jsfiddle.net/meo/TnG4E/

提交回复
热议问题