jQuery trigger event when click outside the element

前端 未结 11 567
无人共我
无人共我 2020-12-29 02:35
$(document).click(function(evt) {
    var target = evt.currentTarget;
    var inside = $(\".menuWraper\");
    if (target != inside) {
        alert(\"bleep\");
             


        
11条回答
  •  盖世英雄少女心
    2020-12-29 03:22

    Just have your menuWraper element call event.stopPropagation() so that its click event doesn't bubble up to the document.

    Try it out: http://jsfiddle.net/Py7Mu/

    $(document).click(function() {
        alert('clicked outside');
    });
    
    $(".menuWraper").click(function(event) {
        alert('clicked inside');
        event.stopPropagation();
    });
    
    • http://api.jquery.com/event.stopPropagation/

    Alternatively, you could return false; instead of using event.stopPropagation();

提交回复
热议问题