要生成一个目录树,首先在您要显示目录的位置,插入以下这段代码:
<table border="0" id="rootNode" style="font-size:9pt;" cellspacing=0 cellpadding=0> <TBODY> </TBODY> </table>
|
然后编写JS代码:
<script language="javascript"> // 创建树的根结点,以后就可以在该根结点下创建子树及叶子 var treeRoot = new tree("rootNode");
var tb; // 子树
// 创建一个子树 tb = new treeBranch( "第一个子树" // 子树显示的文字 ,"../images/timebook_evection.gif" // 子树展开时的图标 ,"../images/timebook_evection.gif" // 子树收缩时的图标 ,null //附加到子树的数据,以后可通过tb.data进行访问 ,"if(this.opened){alert('子树展开');}else{alert('子树收缩');}" // 当子树展开或收缩时将调用的代码 ,"alert('子树被选中!');" // 当子树的文本标签被选中时将调用的代码 );
tb.tdMouseOver = "treeBranchMouseOver"; // 当鼠标移上时子树的样式类名称 tb.tdMouseOut = "treeBranchMouseOut"; // 当鼠标移开时子树的样式类名称
tb.spanMouseOver = "treeBranchMouseOver"; //当鼠标移上时子树文本标签的样式类名称 tb.spanMouseOut = "treeBranchMouseOut"; //当鼠标移开时子树文本标签的样式类名称
tb.spanSelected = "treeBranchSelected"; // 当子树处于选中状态时的样式类名称
// 往根节点添加刚才创建的子树,第一个参数是将要插入的位置 var t = treeRoot.addBranch(treeRoot.length() , tb); // 返回参数 t 为被插入的子树
var tn = t.addNode(t.length() // 将插入的叶子的位置 , "子结点显示名称" // 叶子文本标签文字 , "../images/spacer.gif" // 叶子的显示图标 , "../images/spacer.gif" // 当叶子被选中时将显示的图标 , null // 附加到叶子结点的数据,以后的代码可以通过 tn.data 访问该数据 , "alert('叶子被点中!');" // 当叶子被点击时将调用的代码 ); tn.setClass("disabled"); //设置叶子的样式类名称 // 当鼠标移上时为 disabledMouseOver // 当鼠标移开时为 disabledMouseout
</script>
示例源代码下载: http://www.bqprog.com/sample/treeCreate.rar
请参见: BeQ通用树形控件Ver1.0Beta 
|