简体   繁体   English

单击外部菜单将其关闭

[英]Click outside menu to close it

Here's my function,这是我的功能,

$(document).ready(function () {
   $('.a').click(function () {
     var here = $(this).next('.b');
    if (here.is(":visible")) {
        here.hide();
    } else {
        here.show();
    }
    return false;
  });
});

So, whenever I click the button it opens a small tab on same webpage & whenever I click it again it closes it.因此,每当我单击该按钮时,它都会在同一网页上打开一个小标签,而每当我再次单击它时,它就会关闭它。 But once I open the tab I can't close it by just clicking somewhere on webpage apart from tab.但是一旦我打开标签,我就无法通过点击网页上除标签之外的某个地方来关闭它。 I have to click the button again to close it.我必须再次单击该按钮才能关闭它。

How can I close tab just by clicking somewhere on webpage also by on the button?如何仅通过单击网页上的某处也单击按钮来关闭选项卡?

I end up searching for this on almost every project, so I made this plugin:我最终几乎在每个项目中都搜索了这个,所以我制作了这个插件:

jQuery.fn.clickOutside = function(callback){
    var $me = this;
    $(document).mouseup(function(e) {
        if ( !$me.is(e.target) && $me.has(e.target).length === 0 ) {
            callback.apply($me);
        }
    });
};

It takes a callback function and passes your original selector, so you can do this:它需要一个回调函数并传递您的原始选择器,因此您可以执行以下操作:

$('[selector]').clickOutside(function(){
    $(this).removeClass('active'); // or `$(this).hide()`, if you must
});

Nice, chainable, elegant code.漂亮,可链接,优雅的代码。

On document click, the closest helps to check whether the tab has been clicked or not:单击文档时,最接近的有助于检查选项卡是否已被单击:

$(document).click(function (e) {
    if($('.b').is(':visible')&&!$(e.target).closest('.b').length){
       $('.b').hide();
    }
});

You want to check for a click on the body :你想检查身体上的点击:

$("body").click(function(e) {
  if(e.target.id !== 'menu'){
    $("#menu").hide();
  }      
});

menu would be the id of the menu. menu将是menu的 id。

If the body is clicked and the id of the div clicked doesn't equal that of the menu, then it closes.如果单击主体并且单击的 div 的 id 与菜单的 id 不相等,则它会关闭。

Check this implementation检查这个实现

 jQuery(document).ready(function() { $(document).on('click','body, #btn',function(ev){ ev.stopPropagation() if(ev.target.id== "btn"){ if($('#modal').is(':visible')) { $('#modal').fadeOut(); } else{ $('#modal').fadeIn(); } } else { $('#modal').fadeOut(); } }); });
 html, body { height: 100%; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="btn"> Click Me! </button> <div id="modal" style="background-color:red;display:none;"> BLA BLA BLA </div>

To check if the clicked element is outside of a given container, ie a menu, we can simply check if the event target is a child of the container .要检查被点击的元素是否在给定的容器(即菜单)之外,我们可以简单地检查事件目标是否是容器的子元素。 Using JQuery -使用 JQuery -

$('body').click(function(e) {

    if ( 0 === $(e.target).parents('#container-id').length ) {

        /// clicked outside -> do action

    }

})

you have to add a click listener to the parent element, like here:您必须向父元素添加一个单击侦听器,如下所示:

    $('.parent-div').click(function() {
    //Hide the menus if visible
    });

Also because click events bubbled up from child to the parent, you can exclude the click on the child element to get bubbled up and count as the parent click too.也因为单击事件从子元素冒泡到父元素,您可以排除元素上的单击以冒泡并计为父单击。 you can achieve this like below:你可以像下面这样实现:

    //disable click event on child element 
    $('.child-div').click(function(event){
    event.stopPropagation();
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM