タイトルで。
HTML:
<div class="active"></div>
<div></div>
<div></div>
Javascript (バニラ):
// source HTML collection, only get the elements index which has the class "active"
let theCollection = document.querySelectorAll('div');
// related HTML collection
let anotherCollection = document.querySelectorAll('div .anotherClass');
// adding the class the corresponding element
theCollection[index].classList.add('active');
私が取得する必要があるのは、現在active
内にクラスがある要素のインデックスですtheCollection
。