简体   繁体   English

单击查找父元素的索引(未单击的元素)

[英]On click find index of parent element (not clicked element)

I'm trying to find the index of the.closest element to the clicked element.我正在尝试找到单击元素的 .closest 元素的索引。 My function works to find which button is clicked, but I need to find the index of the parent element.我的 function 可以找到单击了哪个按钮,但我需要找到父元素的索引。

The problem is not all of my parent elements contain this button.问题不是我所有的父元素都包含这个按钮。 This is my code so far:到目前为止,这是我的代码:

 var clickCheck = document.querySelectorAll(".click-check"); Array.prototype.forEach.call(clickCheck, function (checkC, index) { checkC.addEventListener('click', function () { console.log("Clicked button " + index); checkC.closest(".check-parent"); }) });
 .check-parent { background-color: #EC008C; color: #FFF; padding: 10px 20px; margin: 10px 0px; }.click-check { width: 200px; background-color: #000; padding: 5px 20px; }
 <div class="check-parent"> <div class="element"> <div class="click-check">Klik hier</div> </div> </div> <div class="check-parent"> <div class="element"> </div> </div> <div class="check-parent"> <div class="element"> <div class="click-check">Klik hier</div> </div> </div>

The problem here is that when the second parent element with class check-parent doesn't contain the button, it's index is skipped.这里的问题是,当带有 class check-parent的第二个父元素不包含按钮时,它的索引被跳过。 I'm thinking I can use this part checkC.closest(".check-parent");我想我可以使用这部分checkC.closest(".check-parent"); to maybe find like a nodeList and then find the length or something?也许找到一个nodeList然后找到长度或什么?

When I click the second button (in third parent) the console logs index 1 (which is logical).当我单击第二个按钮(在第三个父级中)时,控制台记录索引 1(这是合乎逻辑的)。 How do I find the index of the parent element here?如何在这里找到父元素的索引? So i want it to show index 2 instead of index 1.所以我希望它显示索引 2 而不是索引 1。

I am not sure.我不知道。 Is this implementation fulfill the requirement.这个实现是否满足要求。

 var clickCheck = document.querySelectorAll(".click-check"); Array.prototype.forEach.call(clickCheck, function (checkC, index) { checkC.addEventListener('click', function () { console.log("Clicked button " + checkC.closest(".check-parent").getAttribute("data-index")); ; }) });
 .check-parent { background-color: #EC008C; color: #FFF; padding: 10px 20px; margin: 10px 0px; }.click-check { width: 200px; background-color: #000; padding: 5px 20px; }
 <div> <div class="check-parent" data-index="0"> <div class="element"> <div class="click-check">Klik hier</div> </div> </div> <div class="check-parent" data-index="1"> <div class="element"> </div> </div> <div class="check-parent" data-index="2"> <div class="element"> <div class="click-check">Klik hier</div> </div> </div> </div>

I fixed this issue another way.我用另一种方式解决了这个问题。

I added data-index to <div class="check-parent"> like <div class="check-parent" data-index="0"> and then I used checkC.closest(".check-parent").getAttribute("data-index");我将数据索引添加到<div class="check-parent">就像<div class="check-parent" data-index="0">然后我使用checkC.closest(".check-parent").getAttribute("data-index"); to gain the index of the parent.获取父级的索引。

Thanks for your suggestions!感谢您的建议!

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

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