Вы можете поймать щелчки на родительском элементе ul
и, если это сам li или один из его дочерних, раскрасьте его:
const ulElement = document.querySelectorAll('ul')[0];
ulElement.addEventListener('click', function (event) { // catch all click event on parent ul
if (!event.target || !event.target.parentElement) {return} // if the click not on li or one of the child ignore it
else if (event.target.parentElement.classList.contains('li-class')) { // if clicked element's parent is li with the class
event.target.parentElement.style.color = 'red';
}
else if (event.target.classList.contains('li-class')) { // if clicked element is li with the class
event.target.style.color = 'red'
}
}, false);
<ul>
<li class="li-class" id="0">( ) 1<input class="editor" id="editor0"><input type="checkbox" class="completedButton"><button class="deleteButton" id="deleteButton0">X</button><button class="saveButton" id="saveButton0">save changes</button></li>
<li class="li-class" id="1">( ) 1<input class="editor" id="editor1"><input type="checkbox" class="completedButton"><button class="deleteButton" id="deleteButton1">X</button><button class="saveButton" id="saveButton1">save changes</button></li>
<li class="li-class" id="2">( ) 1<input class="editor" id="editor2"><input type="checkbox" class="completedButton"><button class="deleteButton" id="deleteButton2">X</button><button class="saveButton" id="saveButton2">save changes</button></li>
</ul>
СТАРЫЙ ОТВЕТ:
Я полагаю, что есть много способов добиться такого поведения, это решение отлавливает все события, на которые нажали, и если оно .li-class
сам или ребенок этого класса - измените цвет.
Фрагмент:
document.addEventListener('click', function (event) { // catch all click event on the documnt
if (!event.target || !event.target.parentElement) {return} // if the click is on the document ignore it
else if (event.target.parentElement.classList.contains('li-class')) { // if clicked element's parent is li with the class
event.target.parentElement.style.color = 'red';
}
else if (event.target.classList.contains('li-class')) { // if clicked element is li with the class
event.target.style.color = 'red'
}
}, false);
<ul>
<li class="li-class" id="0">( ) 1<input class="editor" id="editor0"><input type="checkbox" class="completedButton"><button class="deleteButton" id="deleteButton0">X</button><button class="saveButton" id="saveButton0">save changes</button></li>
<li class="li-class" id="1">( ) 1<input class="editor" id="editor1"><input type="checkbox" class="completedButton"><button class="deleteButton" id="deleteButton1">X</button><button class="saveButton" id="saveButton1">save changes</button></li>
<li class="li-class" id="2">( ) 1<input class="editor" id="editor2"><input type="checkbox" class="completedButton"><button class="deleteButton" id="deleteButton2">X</button><button class="saveButton" id="saveButton2">save changes</button></li>
</ul>