简体   繁体   English

Javascript:从点击目标的同级和子级中删除类

[英]Javascript: Remove class from siblings and children of click target

I am trying to build something akin to the OS X Finder. 我正在尝试构建类似于OS X Finder的东西。 Select an element from a top level list and add class to it for styling and to show child list, then select an element of child list. 从顶层列表中选择一个元素,然后向其添加类以进行样式设置和显示子列表,然后选择子列表中的元素。 I have this working. 我有这个工作。 The problem is removing the "active" class from the top level item when a sibling is clicked. 问题是单击同级时从顶级项目中删除“活动”类。 Also, removing "active" from any child list item when a top level item is clicked. 另外,单击顶级项目时,从任何子列表项目中删除“活动”。

In essence: how do I select all siblings of the click even target, and how do I select all children of the click event target. 本质上:如何选择点击均匀目标的所有同级,以及如何选择点击事件目标的所有子代。

I know this would be easy with jquery, but I am not able to use jquery on this project. 我知道使用jquery会很容易,但是我无法在此项目上使用jquery。

Here is what I have so far: 这是我到目前为止的内容:

 var allThings = document.querySelectorAll('.thing'); //all tabs on page function clickHandler(e) { e.preventDefault(); //var siblings = getSiblings(e.target); //console.log("siblings = " + siblings); //for (var i = 0; i < siblings.length; i++) { //siblings[i].classList.remove("active"); //} //e.target.nextSibling.classList.remove("active"); //e.target.previousSibling.classList.remove("active"); e.target.classList.toggle("active"); }; function getSiblings(el, filter) { var siblings = []; el = el.parentNode.firstChild; do { if (!filter || filter(el)) siblings.push(el); } while (el = el.nextSibling); return siblings; } for (var i = 0; i < allThings.length; i++) { allThings[i].onclick = clickHandler; //allThings[i].children.classList.remove("active"); } 
 ul { display: inline-block; vertical-align: top; } .thing { position: relative; background: white; } .thing ul { display: none; } .thing.active { background: #ddd; } .thing.active > ul { display: inline-block; position: absolute; top: 0; left: 100%; } 
 <ul> <li class="thing">First 001 <ul> <li class="thing">Second 001 <ul> <li class="thing">Third 001</li> <li class="thing">Third 002</li> </ul> </li> </ul> </li> <li class="thing">First 002 <ul> <li class="thing">Second 002 <ul> <li class="thing">Third 011</li> <li class="thing">Third 012</li> </ul> </li> </ul> </li> <li class="thing">First 003 <ul> <li class="thing">Second 031 <ul> <li class="thing">Third 031</li> <li class="thing">Third 032</li> </ul> </li> </ul> </li> </ul> 

https://jsfiddle.net/samhadr/ym2Lx7sn/6/ https://jsfiddle.net/samhadr/ym2Lx7sn/6/

A click event should do two things: 点击事件应做两件事:

  1. Remove class active from all sibling and descendant elements. 从所有同级元素和后代元素中删除active类。
  2. Add class active to the clicked element. active类添加到单击的元素。

A querySelectorAll can help with part 1, while part 2 is quite direct: querySelectorAll可以帮助第1部分,而第2部分非常直接:

function clickHandler( e ) {

    e.preventDefault();

    e.target.parentElement.querySelectorAll( ".active" ).forEach( e =>
        e.classList.remove( "active" ) );

    e.target.classList.add( "active" );

};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM