Vue click to add className

  1. For example, the following dom
<li class="del" @click="del($event)">删除</li>
  1. Because js needs to transfer to obtain sibling elements, first obtain the parent element, then obtain the child element, remove the className, and then add the className to the current dom
const siblings = e.currentTarget.parentNode.children;

for (let i = 0; i < siblings.length; i++) {
    
    
  if (siblings[i].classList.contains('active')) {
    
    
    siblings[i].classList.remove('active');
  }
}
e.currentTarget.classList.add('active')

Guess you like

Origin blog.csdn.net/qq_42900469/article/details/131300696