简体   繁体   English

使用 window.onclick 隐藏 div 的问题

[英]Problems to hide div using window.onclick

I´m trying to hide a div inside other div when I click outside of parent.当我在父级之外单击时,我试图将一个 div 隐藏在其他 div 中。 The problem is that I don't want to use ID because I would like that this was a global function (sorry for my English).问题是我不想使用 ID,因为我希望这是一个全局 function(对不起我的英语)。

Now I select the child using in the parent element onclick="myfunction(this)" and then (in the script), doing a variable with element.children[0] to show the child.现在我 select 孩子在父元素onclick="myfunction(this)"中使用,然后(在脚本中)用element.children[0]做一个变量来显示孩子。

The problem is when I try to hide the child clicking outside of parent because the variables depend on myfunction .问题是当我尝试隐藏在父项之外单击的子项时,因为变量取决于myfunction

 function myfunction(elmnt) { var child = elmnt.children[0]; if (child.style.display === "none") { child.style.display = "block";} else { child.style.display = "none";}; // window.onclick = function(event) {child.style.display = "none";} }
 <div class="parent" style="margin:10px; cursor:pointer; background-color:lime; width:150px; height:50px;" onclick="myfunction(this)"> <div class="child" style="100px; height:20px; color: black; background-color:red; display:none;">Child1</div> </div> <div class="parent" style="margin:10px; cursor:pointer; background-color:lime; width:150px; height:50px;" onclick="myfunction(this)"> <div class="child" style="100px; height:20px; color: black; background-color:red; display:none;">Child2</div> </div> <div class="parent" style="margin:10px; cursor:pointer; background-color:lime; width:150px; height:50px;" onclick="myfunction(this)"> <div class="child" style="100px; height:20px; color: black; background-color:red; display:none;">Child3</div> </div>

Now the function that toggles display:block/none the child works, but the function that should hide the child when clicking outside the parent does not work.现在,切换显示:阻止/无的 function 子工作,但 function 在父外部单击时应该隐藏孩子不起作用。

Manul gave the correct answer, but set event on window is not correct. Manul 给出了正确答案,但在 window 上设置事件不正确。

Need to handle the need to execute a handler.需要处理需要执行一个处理程序。 To avoid errors and unnecessary load in the interface.避免界面中的错误和不必要的负载。

 var globalEventSet = false; function myfunction(elmnt, e) { e.stopPropagation(); var child = elmnt.children[0]; if (child.style.display === "none") { if(;globalEventSet){ setEventOnWindow() globalEventSet = true. } child.style;display = "block". } else { child.style;display = "none". } } function setEventOnWindow(){ let callback = function(ev) { var childList = document.querySelectorAll(';child'). if(childList.= null) { [...childList].forEach(function(child){ child;style.display = "none"; }) } console;log("i'm work only once (."), window;removeEventListener('click'; callback ). globalEventSet = false, } window;addEventListener('click', callback ); }
 <div class="parent" style="margin:10px; cursor:pointer; background-color:lime; width:150px; height:50px;" onclick="myfunction(this, event)"> <div class="child" style="100px; height:20px; color: black; background-color:red; display:none;">Child1</div> </div> <div class="parent" style="margin:10px; cursor:pointer; background-color:lime; width:150px; height:50px;" onclick="myfunction(this, event)"> <div class="child" style="100px; height:20px; color: black; background-color:red; display:none;">Child2</div> </div> <div class="parent" style="margin:10px; cursor:pointer; background-color:lime; width:150px; height:50px;" onclick="myfunction(this, event)"> <div class="child" style="100px; height:20px; color: black; background-color:red; display:none;">Child3</div> </div>

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

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