돌아가기
이 글은 다음 언어로만 작성되어 있습니다. English, Español, 日本語, Русский, 简体中文. 한국어 번역에 참여해주세요.

Tree menu

중요도: 5

Create a tree that shows/hides node children on click:

Requirements:

  • Only one event handler (use delegation)
  • A click outside the node title (on an empty space) should not do anything.

샌드박스를 열어 정답을 작성해보세요.

The solution has two parts.

  1. Wrap every tree node title into <span>. Then we can CSS-style them on :hover and handle clicks exactly on text, because <span> width is exactly the text width (unlike without it).
  2. Set a handler to the tree root node and handle clicks on that <span> titles.

샌드박스를 열어 정답을 확인해보세요.