简体   繁体   English

如何在使用JavaScript单击子元素时关闭父div?

[英]How to close the parent div on clicking the child element using JavaScript?

I have four div elements having same class but different Id. 我有四个具有相同类但ID不同的div元素。 Inside each div i have one paragraph element and a span element. 在每个div内,我都有一个段落元素和一个span元素。 The span have same class but different id. 跨度具有相同的类别,但ID不同。 Inside the span i use fa-times(close) icon to close the outer div. 在范围内,我使用fa-times(close)图标关闭外部div。 How to do it with JavaScript? 如何使用JavaScript?

I tried declaring the variables using the class name but it returns an Array of HTML Collection. 我尝试使用类名称声明变量,但它返回HTML集合数组。 However, i am able to hide the parent div by targeting each element from inside the array. 但是,我能够通过从数组内部定位每个元素来隐藏父div。 But I know this is the wrong method. 但是我知道这是错误的方法。

CODE

 function close() { var clost = document.getElementsByClassName('span'); clost[0].parentElement.style.display = 'none'; } 
 <div class="div" id="div_1"> <p>Please Enter a Keyword</p> <span class="span" onclick="close()" id="span_1"> <i class="fa fa-times" aria-hidden="true"></i></span> </div> <div class="div" id="div_2"> <p>Enter your name</p> <span class="span" onclick="close()" id="span_2"> <i class="fa fa-times" aria-hidden="true"></i></span> </div> <div class="div" id="div_3"> <p>Enter your city</p> <span class="span" onclick="close()" id="span_3"> <i class="fa fa-times" aria-hidden="true"></i></span> </div> <div class="div" id="div_4"> <p>Enter your country</p> <span class="span" onclick="close()" id="span_4"> <i class="fa fa-times" aria-hidden="true"></i></span> </div> 

Error : Cannot find parent of Undefined 错误 :找不到未定义的父项

Expected results : All div should close separately on click on the fa-icon." 预期结果 :单击该图标,所有div应单独关闭。”

Firstly close will not work as a function name, as you will get a conflict... so change it to something else like closeDiv . 首先, close不能用作函数名称,因为您会遇到冲突...因此将其更改为closeDiv其他closeDiv

The easiest way to do it with your current code and vanilla JS is to include this as a parameter in the closeDiv() , and then use that directly... 使用当前代码和closeDiv() JS进行操作的最简单方法是将this作为参数包含在closeDiv() ,然后直接使用它...

(Note, I have added the word Text to each span so you actually have something to click on) (请注意,我已经添加了字Text到每个span那么你确实有一些点击)

 function closeDiv(spn) { spn.parentNode.style.display = "none"; } 
 <div class="div" id="div_1"> <p>Please Enter a Keyword</p> <span class="span" onclick="closeDiv(this)" id="span_1"> <i class="fa fa-times" aria-hidden="true">Text</i></span> </div> <div class="div" id="div_2"> <p>Enter your name</p> <span class="span" onclick="closeDiv(this)" id="span_2"> <i class="fa fa-times" aria-hidden="true">Text</i></span> </div> <div class="div" id="div_3"> <p>Enter your city</p> <span class="span" onclick="closeDiv(this)" id="span_3"> <i class="fa fa-times" aria-hidden="true">Text</i></span> </div> <div class="div" id="div_4"> <p>Enter your country</p> <span class="span" onclick="closeDiv(this)" id="span_4"> <i class="fa fa-times" aria-hidden="true">Text</i></span> </div> 


If you have the option to use jQuery, it becomes a bit more simple... 如果您可以选择使用jQuery,它将变得更加简单...

 $(function(){ $(".span").on("click", function(e) { $(this).parent().hide(); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="div" id="div_1"> <p>Please Enter a Keyword</p> <span class="span" id="span_1"> <i class="fa fa-times" aria-hidden="true">Text</i></span> </div> <div class="div" id="div_2"> <p>Enter your name</p> <span class="span" id="span_2"> <i class="fa fa-times" aria-hidden="true">Text</i></span> </div> <div class="div" id="div_3"> <p>Enter your city</p> <span class="span" id="span_3"> <i class="fa fa-times" aria-hidden="true">Text</i></span> </div> <div class="div" id="div_4"> <p>Enter your country</p> <span class="span" id="span_4"> <i class="fa fa-times" aria-hidden="true">Text</i></span> </div> 

 function closeDiv(event) { event.target.parentElement.style.display = 'none'; } 
 span{ background: red } 
 <div class="div" id="div_1"> <p>Please Enter a Keyword</p> <span class="span" onclick="closeDiv(event)" id="span_1">*** <i class="fa fa-times" aria-hidden="true"></i></span> </div> <div class="div" id="div_2"> <p>Enter your name</p> <span class="span" onclick="closeDiv(event)" id="span_2">*** <i class="fa fa-times" aria-hidden="true"></i></span> </div> <div class="div" id="div_3"> <p>Enter your city</p> <span class="span" onclick="closeDiv(event)" id="span_3">*** <i class="fa fa-times" aria-hidden="true"></i></span> </div> <div class="div" id="div_4"> <p>Enter your country</p> <span class="span" onclick="closeDiv(event)" id="span_4">*** <i class="fa fa-times" aria-hidden="true"></i></span> </div> 

May I offer a simpler solution. 我可以提供一个更简单的解决方案。

All your div's for close are the same, so you could use a delegated event. 关闭的所有div都相同,因此可以使用委托事件。 Another advantage is if dynamic content is also added later, these events will work for new elements. 另一个优点是,如果以后也添加动态内容,则这些事件将对新元素起作用。

Some notes, you can attach delegated events at any point your DOM tree. 一些注意事项,您可以在DOM树的任何位置附加委托事件。 Here for a simple example I have just used body . 在这里举一个简单的例子,我刚刚使用了body But if say you had another container that all your controls are in, you could attach there so that your delegated event doesn't fire when not required. 但是,如果说您有另一个包含所有控件的容器,则可以将其附加到该容器中,以便在不需要时不会触发委派事件。

Also if your events need to stop bubbling down the DOM, you can use evt.stopPropagation , and you can even prevent the DOM default behaviour with evt.preventDefault . 此外,如果您的活动需要停止冒泡下来DOM,你可以使用evt.stopPropagation ,你甚至可以防止与DOM默认行为evt.preventDefault

Below is a simple example. 以下是一个简单的示例。

 document.querySelector("body").addEventListener("click", function (evt) { if (evt.target.classList.contains("fa-times")) { evt.target.closest("div").style.display = "none"; } } ); 
 .fa { background-color: #e5e5e5; cursor: pointer; padding: 4px 6px; border: 1px solid silver; border-radius: 3px; } 
 <div> <p>Please Enter a Keyword</p> <span> <i class="fa fa-times" aria-hidden="true">❌</i></span> </div> <div> <p>Enter your name</p> <span> <i class="fa fa-times" aria-hidden="true">❌</i></span> </div> <div> <p>Enter your city</p> <span> <i class="fa fa-times" aria-hidden="true">❌</i></span> </div> <div> <p>Enter your country</p> <span> <i class="fa fa-times" aria-hidden="true">❌</i></span> </div> 

Why don't you pass the id of the parent as argument to the close function? 为什么不将父代的ID作为参数传递给close函数?

<span class="span" onclick="close('div_1')" id="span_1">
<i class="fa fa-times" aria-hidden="true"></i></span>

And javascript: 和javascript:

function close (parentId) {
    var parent = document.getElementById(parentId);
    parent.style.display = 'none';
}

You should tell your close function which div to be closed by giving a parameter to the function. 您应该通过为函数指定参数来告诉close函数关闭哪个div。 Then you can target this element: 然后,您可以定位此元素:

Javascript 使用Javascript

function close(divId) {
  var outerDiv = document.querySelector(`#{divId}`);
  outerDiv.style.display = 'none';
}

HTML HTML

<div class="div" id="div_1">
  <p>Please Enter a Keyword</p>
  <span class="span" onclick="close(div_1)" id="span_1">
<i class="fa fa-times" aria-hidden="true"></i></span>
</div>

<div class="div" id="div_2">
  <p>Enter your name</p>
  <span class="span" onclick="close(div_2)" id="span_2">
<i class="fa fa-times" aria-hidden="true"></i></span>
</div>

<div class="div" id="div_3">
  <p>Enter your city</p>
  <span class="span" onclick="close(div_3)" id="span_3">
<i class="fa fa-times" aria-hidden="true"></i></span>
</div>

<div class="div" id="div_4">
  <p>Enter your country</p>
  <span class="span" onclick="close(div_4)" id="span_4">
<i class="fa fa-times" aria-hidden="true"></i></span>
</div>

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

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