简体   繁体   English

仅在单击特定父级时删除内部元素的实例

[英]Remove instance of inner element only when specific parent is clicked

I have a series of elements where I'd like it when one of the boxes is clicked the inner box on that specific element disappears.我有一系列元素,当单击其中一个框时,我想要它,该特定元素上的内框消失。 I've been playing around with it but can't get it work.我一直在玩它,但无法让它工作。 I'm wondering if I should be using this in the code?我想知道我是否应该在代码中使用this

In the example below I want it so when you click one of the red boxes, that particular box's inner yellow box disappears, but only the yellow box inside the red box that is clicked.在下面的示例中,我想要它,因此当您单击其中一个红色框时,该特定框的内部黄色框会消失,但只有被单击的红色框内的黄色框会消失。

I need to do it with without adding any more classes or IDs to the HTML.我需要在不向 HTML 添加任何类或 ID 的情况下完成此操作。

CodePen: https://codepen.io/emilychews/pen/dyNPwEx代码笔: https://codepen.io/emilychews/pen/dyNPwEx

 var boxOuter = document.querySelectorAll(".box-outer"), boxInner = document.querySelectorAll(".box-inner"); if (boxOuter) { boxOuter.forEach(function (item) { item.addEventListener("click", function () { // code goes here }); }); }
 body { margin: 0; display: flex; justify-content: center; align-items: center; height: 120vh; }.box-outer { position: relative; width: 10rem; height: 5rem; background: red; margin: 1rem; cursor: pointer; }.box-inner { width: 1rem; height: 1rem; background: yellow; position: absolute; bottom: 0.5rem; right: 0.5rem; }
 <main class="main-wrapper"> <div class="box-outer"> <div class="box-inner"></div> </div> <div class="box-outer"> <div class="box-inner"></div> </div> <div class="box-outer"> <div class="box-inner"></div> </div> </main>

This should do the job这应该做的工作

 var boxOuter = document.querySelectorAll(".box-outer"), boxInner = document.querySelectorAll(".box-inner"); if (boxOuter) { boxOuter.forEach(function (item) { item.addEventListener("click", function () { //This will delete everything inside the box... Clear all child nodes this.innerHTML = ''; //Hide inner child //this.children[0].style.visibility = 'hidden'; // code goes here }); }); }
 body { margin: 0; display: flex; justify-content: center; align-items: center; height: 120vh; }.box-outer { position: relative; width: 10rem; height: 5rem; background: red; margin: 1rem; cursor: pointer; }.box-inner { width: 1rem; height: 1rem; background: yellow; position: absolute; bottom: 0.5rem; right: 0.5rem; }
 <main class="main-wrapper"> <div class="box-outer"> <div class="box-inner"></div> </div> <div class="box-outer"> <div class="box-inner"></div> </div> <div class="box-outer"> <div class="box-inner"></div> </div> </main>

Instead of selecting the inner element globally, you could make this inside the listener.您可以在侦听器内部进行,而不是全局选择内部元素。 And then use the setAttribute method to change the display property to 'none'.然后使用setAttribute 方法将显示属性更改为“无”。

 var boxOuter = document.querySelectorAll(".box-outer"); if (boxOuter) { boxOuter.forEach(function (item) { item.addEventListener("click", function () { const boxInner = item.querySelector(".box-inner"); // captures the respective box boxInner.setAttribute("style", "display: none;"); // sets the display attribute }); }); }
 body { margin: 0; display: flex; justify-content: center; align-items: center; height: 120vh; }.box-outer { position: relative; width: 10rem; height: 5rem; background: red; margin: 1rem; cursor: pointer; }.box-inner { width: 1rem; height: 1rem; background: yellow; position: absolute; bottom: 0.5rem; right: 0.5rem; }
 <main class="main-wrapper"> <div class="box-outer"> <div class="box-inner"></div> </div> <div class="box-outer"> <div class="box-inner"></div> </div> <div class="box-outer"> <div class="box-inner"></div> </div> </main>

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

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