This code is about event of 'active' button. I have to use this code globally. But it conflicts with all the elements that have the btn
class.
So I want to give an active class when I click on btn
(button). And when I click on the btn
, I want to find the parents of e.target
, find their children, and move a 'active class'. And I want to use this code globally. But my code is not working. Because It can't find parents of e.target
exactly. Is there any other good way?
var btns = document.querySelectorAll('.btn');
[].forEach.call(btns, function (item) {
item.addEventListener('click', function (e) {
var selectedParent = e.target.parentNode;
var selected = selectedParent.querySelectorAll('.btn.active');
if (selected.length > 0) {
selected[0].className = selected[0].className.replace(' active', '');
item.className += ' active';
}
});
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…