如何在单击子按钮时将"活动的"切换类添加到父div.我可以在jQuery中做到这一点,但我想使用纯Java脚本.
为了更好地理解,下面是示例html代码片段.
<div class="parent-div">
<div class="title-of-the-section">Section title</div>
<button class="toggle-class-button">Add "active" class to parent div of ['parent-div']</button>
</div>
<div class="parent-div">
<div class="title-of-the-section">Section title</div>
<button class="toggle-class-button">Add "active" class to parent div of ['parent-div']</button>
</div>
<div class="parent-div">
<div class="title-of-the-section">Section title</div>
<button class="toggle-class-button">Add "active" class to parent div of ['parent-div']</button>
</div>
<div class="parent-div">
<div class="title-of-the-section">Section title</div>
<button class="toggle-class-button">Add "active" class to parent div of ['parent-div']</button>
</div>
我不想使用document.getElementById('')
,因为将有几个部分,在每个部分将保持按钮,以添加触发类.
我try 了其他一些示例,但它们是基于ID而不是类的,我不能重复相同的ID,它也不起作用.