请教树型菜单的高手!

水煮蛙 2003-12-15 12:10:21
为什么该代码中结点名只能用数字,用字母+数字就出错!

/**************************************************************************
Copyright (c) 2001-2003 Geir Landr?(drop@destroydrop.com)
JavaScript Tree - www.destroydrop.com/hjavascripts/tree/
Version 0.96

This script can be used freely as long as all copyright messages are
intact.
**************************************************************************/

// Arrays for nodes and icons
var nodes = new Array();;
var openNodes = new Array();
var icons = new Array(6);

// Loads all icons that are used in the tree
function preloadIcons() {
icons[0] = new Image();
icons[0].src = "img/plus.gif";
icons[1] = new Image();
icons[1].src = "img/plusbottom.gif";
icons[2] = new Image();
icons[2].src = "img/minus.gif";
icons[3] = new Image();
icons[3].src = "img/minusbottom.gif";
icons[4] = new Image();
icons[4].src = "img/folder.gif";
icons[5] = new Image();
icons[5].src = "img/folderopen.gif";
}
// Create the tree
function createTree(arrName, startNode, openNode) {
nodes = arrName;
if (nodes.length > 0) {
preloadIcons();
if (startNode == null) startNode = 0;
if (openNode != 0 || openNode != null) setOpenNodes(openNode);

if (startNode !=0) {
var nodeValues = nodes[getArrayId(startNode)].split("|");
document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\"><img src=\"img/folderopen.gif\" align=\"absbottom\" alt=\"\" />" + nodeValues[2] + "</a><br />");
} else document.write("<img src=\"img/base.gif\" align=\"absbottom\" alt=\"\" />Website<br />");

var recursedNodes = new Array();
addNode(startNode, recursedNodes);
}
}
// Returns the position of a node in the array
function getArrayId(node) {
for (i=0; i<nodes.length; i++) {
var nodeValues = nodes[i].split("|");
if (nodeValues[0]==node) return i;
}
}
// Puts in array nodes that will be open
function setOpenNodes(openNode) {
for (i=0; i<nodes.length; i++) {
var nodeValues = nodes[i].split("|");
if (nodeValues[0]==openNode) {
openNodes.push(nodeValues[0]);
setOpenNodes(nodeValues[1]);
}
}
}
// Checks if a node is open
function isNodeOpen(node) {
for (i=0; i<openNodes.length; i++)
if (openNodes[i]==node) return true;
return false;
}
// Checks if a node has any children
function hasChildNode(parentNode) {
for (i=0; i< nodes.length; i++) {
var nodeValues = nodes[i].split("|");
if (nodeValues[1] == parentNode) return true;
}
return false;
}
// Checks if a node is the last sibling
function lastSibling (node, parentNode) {
var lastChild = 0;
for (i=0; i< nodes.length; i++) {
var nodeValues = nodes[i].split("|");
if (nodeValues[1] == parentNode)
lastChild = nodeValues[0];
}
if (lastChild==node) return true;
return false;
}
// Adds a new node to the tree
function addNode(parentNode, recursedNodes) {
for (var i = 0; i < nodes.length; i++) {

var nodeValues = nodes[i].split("|");
if (nodeValues[1] == parentNode) {

var ls = lastSibling(nodeValues[0], nodeValues[1]);
var hcn = hasChildNode(nodeValues[0]);
var ino = isNodeOpen(nodeValues[0]);

// Write out line & empty icons
for (g=0; g<recursedNodes.length; g++) {
if (recursedNodes[g] == 1) document.write("<img src=\"img/line.gif\" align=\"absbottom\" alt=\"\" />");
else document.write("<img src=\"img/empty.gif\" align=\"absbottom\" alt=\"\" />");
}

// put in array line & empty icons
if (ls) recursedNodes.push(0);
else recursedNodes.push(1);

// Write out join icons
if (hcn) {
if (ls) {
document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 1);\"><img id=\"join" + nodeValues[0] + "\" src=\"img/");
if (ino) document.write("minus");
else document.write("plus");
document.write("bottom.gif\" align=\"absbottom\" alt=\"Open/Close node\" /></a>");
} else {
document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 0);\"><img id=\"join" + nodeValues[0] + "\" src=\"img/");
if (ino) document.write("minus");
else document.write("plus");
document.write(".gif\" align=\"absbottom\" alt=\"Open/Close node\" /></a>");
}
} else {
if (ls) document.write("<img src=\"img/joinbottom.gif\" align=\"absbottom\" alt=\"\" />");
else document.write("<img src=\"img/join.gif\" align=\"absbottom\" alt=\"\" />");
}

// Start link
document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\">");

// Write out folder & page icons
if (hcn) {
document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"img/folder")
if (ino) document.write("open");
document.write(".gif\" align=\"absbottom\" alt=\"Folder\" />");
} else document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"img/page.gif\" align=\"absbottom\" alt=\"Page\" />");

// Write out node name
document.write(nodeValues[2]);

// End link
document.write("</a><br />");

// If node has children write out divs and go deeper
if (hcn) {
document.write("<div id=\"div" + nodeValues[0] + "\"");
if (!ino) document.write(" style=\"display: none;\"");
document.write(">");
addNode(nodeValues[0], recursedNodes);
document.write("</div>");
}

// remove last line or empty icon
recursedNodes.pop();
}
}
}
// Opens or closes a node
function oc(node, bottom) {
var theDiv = document.getElementById("div" + node);
var theJoin = document.getElementById("join" + node);
var theIcon = document.getElementById("icon" + node);

if (theDiv.style.display == 'none') {
if (bottom==1) theJoin.src = icons[3].src;
else theJoin.src = icons[2].src;
theIcon.src = icons[5].src;
theDiv.style.display = '';
} else {
if (bottom==1) theJoin.src = icons[1].src;
else theJoin.src = icons[0].src;
theIcon.src = icons[4].src;
theDiv.style.display = 'none';
}
}
// Push and pop not implemented in IE
if(!Array.prototype.push) {
function array_push() {
for(var i=0;i<arguments.length;i++)
this[this.length]=arguments[i];
return this.length;
}
Array.prototype.push = array_push;
}
if(!Array.prototype.pop) {
function array_pop(){
lastElement = this[this.length-1];
this.length = Math.max(this.length-1,0);
return lastElement;
}
Array.prototype.pop = array_pop;
}
...全文
37 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jimmhua 2004-01-06
  • 打赏
  • 举报
回复
我开发的是树形弹出式菜单,采用的是数组,相关的技术的数据的栈式存贮,没你的这么长,就象应用程序的菜单一样。但无图形。
很不喜欢看你的这么长的程序。仅作提示。
laoniuwudi 2004-01-06
  • 打赏
  • 举报
回复
我这里倒有一个很好的TreeMenu,应该是老外写的,我拿到的不是原版,他是结合COOKIE来实现的,这样就不用管它NETSCAPE的兼容性了!
要的话给我留言,我的空间有流量限制,呵呵,舍不得!
i三千 2004-01-05
  • 打赏
  • 举报
回复
up~
liuruhong 2004-01-05
  • 打赏
  • 举报
回复
很长时间的帖子了,楼主似乎给我发过email,不过我一直没有时间修改,很抱歉
jiayuanliao 2004-01-05
  • 打赏
  • 举报
回复
哈哈,我从来不看短消息的?!!
水煮蛙 2003-12-15
  • 打赏
  • 举报
回复
to : Programmersheaven(Finally) 这个网站我已经看过了!不过还是谢谢!

我现在主要是想对实现原理进行了解,同时解决我上面的问题!
Programmersheaven 2003-12-15
  • 打赏
  • 举报
回复
http://fason.nease.net/
liuruhong 2003-12-15
  • 打赏
  • 举报
回复
ps:忘记了,你似乎要实现的就是tree这个东西吧,偶也正在写这个方面的东西,可以参考参考,不过代码有点长
http://blueswing.heiyou.com/newtree.zip
liuruhong 2003-12-15
  • 打赏
  • 举报
回复
稍微看了一下你的code,你最好贴出完整的sample,不然分析那个代码太太的类了

至于实现的原理,主要是想了解DHTML得实现,还是算法,还是JavaScript的OOP?也许我能够给你一点回答
水煮蛙 2003-12-15
  • 打赏
  • 举报
回复
急 啊! 分不够可以在加!!
shg918 2003-12-15
  • 打赏
  • 举报
回复
mark
水煮蛙 2003-12-15
  • 打赏
  • 举报
回复
顶!

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧