简体   繁体   English

在 div1 和 div2 外部单击时运行 Javascript function

[英]Run a Javascript function when clicking outside div1 AND div2

Trying to run a Javascript function only if click outside div1 AND div2 .只有在div1 AND div2外部单击时,才尝试运行 Javascript function 。 If clicked inside div1 or div2, nothing should happen.如果在 div1 或 div2 内单击,则不会发生任何事情。 Any idea how I could achieve that?知道我怎么能做到这一点吗? Example: I would need div2 to be set display: none and additionally add styles to div1 when clicked outside the divs.示例:我需要将div2设置为display: none并在 div 外部单击时另外将 styles 添加到div1

 var x = document.getElementById("mega-menu"); document.addEventListener('mouseup', function(e) { if (.(x.contains(e.target) || document.getElementById("menu-item-136").contains(e.target))) { x.style;display = 'none'. var elem = document;querySelectorAll("#menu-item-136"). elem.forEach((elem) => { elem.style;backgroundColor = "red"; }); } });

This is the furthest I could get.这是我能得到的最远距离。 But at the moment, div2 (mega-menu) also gets hidden when clicked on the menu item... Hope you understand what I mean...但是目前,当单击菜单项时,div2(mega-menu)也被隐藏了......希望你明白我的意思......

Thanks谢谢

Can you complete your sample code above?你能完成上面的示例代码吗? I've added elements to your snippet, but can't duplicate your problem.我已在您的代码段中添加了元素,但无法复制您的问题。 It seems to be working for me.它似乎对我有用。

Also a couple of off-topic best practices:还有一些离题的最佳实践:

  • the HTML standard doesn't allow for elements with duplicate IDs. HTML 标准不允许具有重复 ID 的元素。 If you have more than one element with id #menu-item-136 , you should be using a class instead of an ID.如果您有多个 ID #menu-item-136元素,则应使用 class 而不是 ID。
  • Get both elements outside the event listener...more efficient (like you're already doing with #mega-menu )在事件侦听器之外获取两个元素......更高效(就像你已经在使用#mega-menu一样)
  • Instead of having an if statement with a nested block, do an if...return instead.与其使用带有嵌套块的 if 语句,不如使用 if...return 代替。 That way your code is less indented.这样你的代码就少了缩进。

 var x = document.getElementById('mega-menu'); var y = document.getElementById('menu-item-136'); document.addEventListener('mouseup', function(e) { if (x.contains(e.target) || y.contains(e.target)) return; x.style.display = 'none'; y.style.backgroundColor = 'red'; });
 div { border: 1px solid black; }
 <div id='mega-menu'>mega menu</div> <div id='menu-item-136'>menu 136</div>

Looks like from this code,从这段代码看起来,

document.querySelectorAll("#menu-item-136");

There are multiple elements with id "menu-item-136" as you are looping through the result.当您循环浏览结果时,有多个 id 为“menu-item-136”的元素。

First of all If there are multiple elements with same Id, It is not a valid HTML.首先,如果有多个元素具有相同的 Id,则它不是有效的 HTML。

Thats why这就是为什么

document.getElementById("menu-item-136").contains(e.target)

this results in false, as it may have selected wrong element other that what you have clicked.这会导致错误,因为它可能选择了错误的元素,而不是您单击的元素。

Even if you use class to get the elements, it may still fail as querySelector based on class will still results in returning multiple elements.即使您使用 class 来获取元素,它仍然可能会失败,因为基于 class 的 querySelector 仍然会导致返回多个元素。

Instead of checking if your target element is contained in the div's - use a check to see if the element parent hierarchy have class name that you add for menu-item div.而不是检查您的目标元素是否包含在 div 中 - 使用检查元素父层次结构是否具有您为菜单项 div 添加的 class 名称。

if( e.target.closest(".container-class")) if(e.target.closest(".container-class"))

So now you are just checking if current target have any parent with menu-item's class.所以现在你只是检查当前目标是否有任何带有菜单项 class 的父级。

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

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