当用户单击“i”元素时,我正在尝试在“p”元素上添加“活动”类。我试图遍历“p”元素,但它为所有“p”元素添加了类。我想要的是将类“活动”添加到仅单击“i”的“p”元素。此外,如果任何“p”元素上存在一个“活动”类,我还需要删除它。这是示例代码
window.onload = function () {
let y = document.getElementsByClassName("fa");
for (let i = 0; i < y.length; i++) {
y[i].addEventListener('click', classWork);
}
function classWork(e) {
let z = document.getElementsByTagName("p");
for (i = 0; i < z.length; i++) {
z[i].classList.add("active");
}
}
}
<div id="otherele" class="m-design">
<li class="first-1">
<h4>CHOOSE YOUR STYLE</h4> <span class="info">
<i class="fa fa-info-circle"></i></span>
<p>Select from full selection of Suites, Jackets, Shirts and much more to <br>get your best look
</p>
</li>
<li class="first-1">
<h4>CUSTOMIZE</h4> <span class="info" id="text-update">
<i class="fa fa-info-circle"></i></span>
<p>You may customize every detail of your garment and we will make exactly how you want it. You can
customize Buttons, Monogram, Lining, Pant style etc.
</p>
</li>
</div>