简体   繁体   English

当我在菜单外单击时如何隐藏多个jQuery toggle()?

[英]How to hide multiple jQuery toggle() when I click outside of the menu?

I have multiple toggle elements on my page. 我的页面上有多个切换元素。 I am trying to get them closed when clicking on the outside of the div element. 我试图在div元素的外部单击时将它们关闭。 the script i have now works veyr well with multiple elements but it also closes the div when clicking on inside of the div 我现在使用的脚本可以很好地处理多个元素,但是在div上单击时,它也会关闭div

   <ul>      

<li class="menuContainer">
  <a class="top" href="#">Menu</a>   
  <div class="sub"> 
    <a class="top" href="google.com">item in dropdown menu with valid   url when clicked here div.sub should stay close</a>
 </div>
</li>
 <li class="menuContainer">
   <a class="top" href="#">Menu</a>   
    <div class="sub"> 
     <a class="top" href="#">item in dropdown menu when clicked here div.sub should stay open</a>
   </div>
</li>  

$(document).ready(function(){

$('li.menuContainer').each(function() {
var $dropdown = $(this);

$("a.top", $dropdown).click(function(e) {
  e.preventDefault();
  $div = $("div.sub", $dropdown);
  $div.toggle();
  $("li.menuContainer div.sub").not($div).hide();
  $( "#effect" ).hide();
  return false;
});
 $("li.menuContainer div.sub").on("click", function (event) {
    event.stopPropagation();
});

});



$(document).on("click", function (){
  $("li.menuContainer div.sub").hide();

});

});

what i want to do is to stop closing div when clicked inside of it. 我想做的是当在其内部单击时停止关闭div。 Any help please? 有什么帮助吗?

I think you are looking for e.stopPropagation(); 我认为您正在寻找e.stopPropagation(); . Use it in an event on the child items. 在子项上的事件中使用它。

This will stop the event from propagating to the parent div. 这将阻止事件传播到父div。

use this code 使用此代码

$("li.menuContainer > a.top").click(function(e) {
  e.preventDefault();
  $div = $(this).next("div.sub");
  $("li.menuContainer div.sub").not($div).slideUp();
  $div.slideDown();
});

instead of 代替

$('li.menuContainer').each(function() {
 var $dropdown = $(this);

 $("a.top", $dropdown).click(function(e) {
  e.preventDefault();
  $div = $("div.sub", $dropdown);
  $div.toggle();
  $("li.menuContainer div.sub").not($div).hide();

  return false;
 });

});

and about 和关于

$('html').click(function(){
  $("li.menuContainer div.sub").hide();
});

you can use 您可以使用

$(window).on('click',function(e){
   if (!$("li.menuContainer").is(e.target) // if the target of the click isn't the container...
      && $("li.menuContainer").has(e.target).length === 0) // ... nor a descendant of the container
  {
     $("li.menuContainer div.sub").slideUp();
  }
});

Working Demo 工作演示

Update: 更新:

$("div.sub > a.top").on('click',function(e) {
    e.preventDefault(); // prevent page from reloading
    e.stopPropagation(); // prevent parent click
    alert('Here We Are :)');
  });

Working Demo 工作演示

In your code the problem is inside the following function, now corrected: 在您的代码中,问题出在以下函数内,现在已纠正:

$('html').click(function(event){
    var ele = event.target.tagName + '.' + event.target.className;
    if (ele != 'DIV.sub') {
        $("li.menuContainer div.sub").hide();
    }
});

Like you can see now when you click on whatever html element a check is done to prevent the undesired action. 就像您现在看到的那样,当您单击任何html元素时,都进行了检查以防止执行不希望的操作。

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

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