简体   繁体   English

单击某个块时,如何不计算子块(例如.child span)?

[英]How can I not count an child block (example .child span) when clicking on a block?

How can I not count a child block (example .child span ) when clicking on a block? 单击某个块时,如何不计算子块(例如.child span )?

If you click on the block .child it's fine. 如果单击.child块, .child很好。 But if your click hits the block .child span there's an error 但是,如果您的点击击中了.child span ,则会出现错误

 var parent = document.querySelector('.parent'); var childLength = document.querySelectorAll('.child').length; var countClick = 0; parent.onclick = function(event) { var target = event.target; if (target.className == 'child') { target.className = 'child child-click'; setTimeout(function() { target.className = 'child child-hide'; }, 3000); } console.log(countClick); countClick++; if (childLength == countClick) { setTimeout(function() { parent.innerHTML = 'All right'; }, 3000); } } 
 .parent { display: flex; } .child { padding: 15px; background: #ccc; margin: 10px; } .child-click { background: #f00; } .child-hide { display: none; } 
 <div class="parent"> <div class="child"><span>1</span></div> <div class="child"><span>2</span></div> <div class="child"><span>3</span></div> <div class="child"><span>4</span></div> </div> 

Here's my code on JSFiddle . 这是我在JSFiddle上的代码。

How can this problem be solved? 如何解决这个问题?

Thank you. 谢谢。 I will be glad to any help. 我将很高兴为您提供任何帮助。

Get the closest element which has a .child class with .closest() like so: 使用.closest()获取具有.child类的最近元素,如下所示:

var target = event.target.closest('.child');

 var parent = document.querySelector('.parent'); var childLength = document.querySelectorAll('.child').length; var countClick = 0; parent.onclick = function(event) { if(event.target === parent) { console.log("return cause it's the parent"); return; } var target = event.target.closest('.child'); if (target.className == 'child') { target.className = 'child child-click'; setTimeout(function() { target.className = 'child child-hide'; }, 3000); } console.log(countClick); countClick++; if (childLength == countClick) { setTimeout(function() { parent.innerHTML = 'All right'; }, 3000); } } 
 .parent { display: flex; } .child { padding: 15px; background: #ccc; margin: 10px; } .child-click { background: #f00; } .child-hide { display: none; } 
 <div class="parent"> <div class="child"><span>1</span></div> <div class="child"><span>2</span></div> <div class="child"><span>3</span></div> <div class="child"><span>4</span></div> </div> 

is that what you want? 那是你要的吗?

Option 2 Have a look at this. 选项2看看这个。 It's another how you could solve your problems with setting the event listener directly to the child elements. 这是通过直接将事件侦听器设置为子元素来解决问题的另一种方法。 Code should be self explanatory but leave a comment if something is not clear. 代码应具有自我解释性,但如果不清楚,请发表评论。

 var elements = document.querySelectorAll('.child'); var countClick = 0; elements.forEach(function(element){ element.addEventListener('click', function(){ element.classList.add('child-click'); setTimeout(function() { element.classList.add('child-hide'); }, 3000); console.log(countClick); countClick++; if (elements.length == countClick) { setTimeout(function() { element.parentNode.innerHTML = 'All right'; }, 3000); } }); }); 
 .parent { display: flex; } .child { padding: 15px; background: #ccc; margin: 10px; } .child-click { background: #f00; } .child-hide { display: none; } 
 <div class="parent"> <div class="child"><span>1</span></div> <div class="child"><span>2</span></div> <div class="child"><span>3</span></div> <div class="child"><span>4</span></div> </div> 

You can check if the target is span or not. 您可以检查目标是否为span If target is span change the target to it's parent: 如果目标是span则将目标更改为其父级:

 var parent = document.querySelector('.parent'); var childLength = document.querySelectorAll('.child').length; var countClick = 0; parent.onclick = function(event) { var target = event.target; target = target.nodeName == 'SPAN'? target.parentNode : target; if (target.className == 'child') { target.className = 'child child-click'; setTimeout(function() { target.className = 'child child-hide'; }, 3000); } console.log(countClick); countClick++; if (childLength == countClick) { setTimeout(function() { parent.innerHTML = 'All right'; }, 3000); } } 
 .parent { display: flex; } .child { padding: 15px; background: #ccc; margin: 10px; } .child-click { background: #f00; } .child-hide { display: none; } 
 <div class="parent"> <div class="child"><span>1</span></div> <div class="child"><span>2</span></div> <div class="child"><span>3</span></div> <div class="child"><span>4</span></div> </div> 

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

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