运行效果如下:
jquery创建树形目录-LMLPHP
代码如下,已做注释。

点击(此处)折叠或打开

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2.   
  3. "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <script src="http://code.jquery.com/jquery-latest.js"></script>
  7. <link rel="stylesheet"
  8.     href="http://jquery.bassistance.de/treeview/demo/screen.css"
  9.     type="text/css" />
  10. <link rel="stylesheet"
  11.     href="http://jquery.bassistance.de/treeview/jquery.treeview.css"
  12.     type="text/css" />
  13. <script type="text/javascript"
  14.     src="http://jquery.bassistance.de/treeview/jquery.treeview.js"></script>
  15. <script>
  16.     $(document).ready(function() {
  17.         //id一定要放在span中,不能放在ul或li中,否则会让一块的颜色都变掉,如果想要在点击事件中做其它事情,重写$("span").click(function()函数就可以了
  18.         //基本思想是,树形目录的每一个id都不一样,对应数据库中的条目id,点击后,先获取被点击元素的id,然后根据id做其它相应的处理
  19.         $("#example").treeview();
  20.         $("span").click(function() {
  21.             //$("span.now").css("background-color", "");
  22.             //下面两语配合css实现选中的当前项变蓝色,但是怎么让背景变色,而不是让字体变色呢?
  23.             //同时这两语还可以获取上一次点击的项和当前项id,以便对点击做进一步处理
  24.             $("span.current").removeClass("current");
  25.             $(this).addClass("current");

  26.         })

  27.     });
  28. </script>
  29. <style type="text/css">
  30. .filetree span.current {
  31.         color:blue;
  32.         background-color:green;
  33. }
  34. #outer {
  35.   width:300px;
  36.   
  37. border-style: solid;
  38. border-width: 1px


  39. }
  40. </style>

  41. </head>
  42. <body>
  43. <div id="outer">
  44.     <ul id="example" class="filetree">
  45.         <li><span id=1 class="folder">Folder 1</span>
  46.             <ul>
  47.                 <li><span id=1.1 class="file">Item 1.1</span></li>
  48.             </ul></li>
  49.         <li><span id=2 class="folder">Folder 2</span>
  50.             <ul>
  51.                 <li><span id=2.1 class="folder">Subfolder 2.1</span>
  52.                     <ul>
  53.                         <li><span id=2.1.1 class="file">File 2.1.1</span></li>
  54.                         <li><span id=2.1.2 class="file">File 2.1.2</span></li>
  55.                     </ul></li>
  56.                 <li><span id=2.2 class="file">File 2.2</span></li>
  57.             </ul></li>
  58.         <li class="closed"><span id=3 class="folder">Folder 3
  59.                 (closed at start)</span>
  60.             <ul>
  61.                 <li><span id=3.1 class="file">File 3.1</span></li>
  62.             </ul></li>
  63.         <li><span id=4 class="file">File 4</span></li>
  64.     </ul>
  65. </div>
  66. </body>
  67. </html>


09-09 03:04