如何使用 CSS 和 JavaScript 建立樹狀檢視?


在一個網頁上,如果你想要展示資料夾檢視,如同在網路託管檔案中展示那樣,那麼就建立一個樹狀檢視。在樹狀檢視中,根或主目錄總是可單擊的。我們使用 cursor 屬性將值設為 pointer 來設定它可單擊。單擊時箭頭鍵會旋轉 90 度。這是使用 transform 屬性中的 rotate() 方法實現的。

設定樹狀檢視的根

使用 <ul> 設定樹狀檢視的根。 在其中,設定 <span> −

<ul id="treeUL">
   <li>
      <span class="rootTree">Root</span>
      <!--   set other tree view root and children -->
   </li>
</ul>

設定樹狀檢視的根樣式。將游標設定為 pointer,以使根看起來像是可單擊的 −

.rootTree {
   cursor: pointer;
   user-select: none;
   font-size: 18px;
   font-weight: bold;
   color: blue;
}

在根中設定樹狀檢視的子內容

使用 <ul> 設定子內容−

<ul class="children">
   <li>/bin</li>
   <li>/etc</li>
</ul>

根的箭頭鍵

要設定箭頭鍵,請使用 content 屬性並在其中提及程式碼 −

.rootTree::before {
   content: "\25B6";
   color: black;
   display: inline-block;
   margin-right: 6px;
}

點選根

點選根後,它會旋轉,因為 rotate() 方法已經設定為 90 度 −

.rootTree-down::before {
   transform: rotate(90deg);
}

示例

要使用 CSS 和 JavaScript 建立樹狀檢視,程式碼如下 −

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      }
      ul,
      #treeUL {
         list-style-type: none;
      }
      #treeUL {
         margin: 0;
         padding: 0;
      }
      .rootTree {
         cursor: pointer;
         user-select: none;
         font-size: 18px;
         font-weight: bold;
         color: blue;
      }
      li {
         font-size: 16px;
         color: crimson;
         font-weight: 500;
      }
      .rootTree::before {
         content: "\25B6";
         color: black;
         display: inline-block;
         margin-right: 6px;
      }
      .rootTree-down::before {
         transform: rotate(90deg);
      }
      .children {
         display: none;
      }
      .active {
         display: block;
      }
   </style>
</head>
<body>
   <h1>Tree view example</h1>
   <ul id="treeUL">
      <li>
         <span class="rootTree">Root</span>
         <ul class="children">
            <li>/bin</li>
            <li>/etc</li>
            <li>
               <span class="rootTree">/home</span>
               <ul class="children">
                  <li>/home/Downloads</li>
                  <li>/home/Pictures/</li>
                  <li>
                     <span class="rootTree">/home/Desktop</span>
                     <ul class="children">
                        <li>/home/Desktop/file.txt</li>
                        <li>/home/Desktop/file1.mp3</li>
                        <li>/home/Desktop/file1.mp4</li>
                     </ul>
                  </li>
               </ul>
            </li>
         </ul>
      </li>
   </ul>
   <script>
      debugger;
      console.log('wow');
      var toggler = document.querySelectorAll(".rootTree");
      Array.from(toggler).forEach(item => {
         item.addEventListener("click", () => {
            item.parentElement
            .querySelector(".children")
            .classList.toggle("active");
            item.classList.toggle("rootTree-down");
         });
      });
   </script>
</body>
</html>

更新於: 14-Dec-2023

1K+ 瀏覽量

開啟您的職業生涯

完成課程即可獲得認證

開始學習
廣告