Step By Step 制作XML的Javascript树形目录(一)

Step By Step 制作XML的Javascript树形目录(一)

很多地方都会用到树形目录,比如CSDN论坛的列表,这样的代码也有很多,但是很多人都是拿来主义,没有自己动手做个,下面我就和大家一起分享怎么来自己做一个XML做数据源的TreeMenu。从中你会看到很多有用的JS脚本和页面元素的一些重要但经常被我们忽略的属性用法。

Step1.倒着来,看看完成后的TreeMenu是什么样子的?

JS脚本动态生成的HTML大纲,这个是动态生成的,页面源代码里没有

 1<div id="MyDiv">
 2<span class="hasItems" expanding="true" id="csxmlTree" style="MARGIN-LEFT: 0px" target="" text="根目录" treeid="">
 3<img src="images/contract.gif"/>根目录</span><br/>
 4<span style="  DISPLAY: none  "><span class="hasItems" expanding="false" id="csxmlTree" style="MARGIN-LEFT: 16px" target="" text="目录1" treeid="1000">
 5<img src="images/contract.gif"/>目录1</span><br/>
 6<span class="Items" expanding="false" href="javascript:alert(this.innerHTML);" id="csxmlTree" style="MARGIN-LEFT: 32px" target="" text="目录1.1" treeid="2000">
 7<img src="images/endnode.gif"/>目录1.1</span><br/>
 8<span class="Items" expanding="false" href="1.htm" id="csxmlTree" style="MARGIN-LEFT: 32px" target="_blank" text="目录1.2" treeid="2001">
 9<img src="images/endnode.gif"/>目录1.2</span><br/>
10<span class="hasItems" expanding="false" id="csxmlTree" style="MARGIN-LEFT: 16px" target="" text="目录2" treeid="3000">
11<img src="images/contract.gif"/>目录2</span><br/>
12<span class="Items" expanding="false" id="csxmlTree" style="MARGIN-LEFT: 32px" target="" text="目录2.1" treeid="3001">
13<img src="images/endnode.gif"/>目录2.1</span><br/>
14<span class="Items" expanding="false" id="csxmlTree" style="MARGIN-LEFT: 32px" target="" text="目录2.2" treeid="3002">
15<img src="images/endnode.gif"/>目录2.2</span><br/>
16<span class="Items" expanding="false" id="csxmlTree" style="MARGIN-LEFT: 32px" target="" text="目录2.3" treeid="3003">
17<img src="images/endnode.gif"/>目录2.3</span><br/>
18<span class="Items" expanding="false" id="csxmlTree" style="MARGIN-LEFT: 32px" target="" text="目录2.3" treeid="3004">
19<img src="images/endnode.gif"/>目录2.3</span><br/>
20<span class="hasItems" expanding="false" id="csxmlTree" style="MARGIN-LEFT: 16px" target="" text="目录3" treeid="4000">
21<img src="images/contract.gif"/>目录3</span><br/>
22<span class="Items" expanding="false" id="csxmlTree" style="MARGIN-LEFT: 32px" target="" text="目录3.1" treeid="4001">
23<img src="images/endnode.gif"/>目录3.1</span><br/>
24<span class="Items" expanding="false" id="csxmlTree" style="MARGIN-LEFT: 16px" target="" text="目录4" treeid="5000">
25<img src="images/endnode.gif"/>目录4</span><br/>
26</span>
27</div>

上面的代码是不是看起来很乱?其实不是的,你把它粘贴到FP等可视化工具的代码里面,就能看清楚结构了。其实很简单,就是SPAN网页Tag生成一格嵌套的结构。我们接下来要写的JS脚本的主要目的就是读取XML文件然后生成这样的结构。所以这个是关键,你一定要看明白。不妨把它先存成一个网页,看看效果(注意,原封不动拷贝的话,只能显示“根目录”一项,你需要改动蓝色的地方,把none去掉),除了红色的DIV其他都是JS脚本动态生成的。class="Items"和class="hasItems"是类似这样的样式表单:

.hasItems {font-weight:bold;height:20px;padding:3 6 0 6;margin:2px;cursor:hand;color:#555555;border:1px solid #fffffa;}
.Items {height:20px;padding:3 6 0 6;margin:1px;cursor:hand;color:#555555;border:1px solid #fffffa;}

察看黄色部分的样式表,可以明白Tree结构形式上就是靠这个定义左边距来达到效果了,通过缩紧不同的量,这样就会很清楚把树形构造出来了。

接下来要写的脚本就是要通过一定的方法把上面提到的要点构造出HTML代码来。大家仔细把上面的弄明白,下面我们就开工!!!

Published At
Categories with Web编程
Tagged with
comments powered by Disqus