做一个菜单,需要读取服务器端的XML文件,然后根据XML文件内容拼成菜单.
读取XML
var docMenu=new ActiveXObject("Microsoft.XMLDOM"); //创建XmlDom对象
docMenu.async=true; //使用异步加载
docMenu.onreadystatechange=LoadedMenu;
function LoadMenuDoc()
{
var Url="Xml/pageMenu.xml";
docMenu.load(Url);
}
function LoadedMenu()
{
var txt="<table cellspacing=\"1\" class=\"grid fixed\"><tr><td class=\"category hand\">未加载!</td></tr></table>"
if(docMenu.readyState==4)
{
txt = "";
if(docMenu.parseError.errorCode != 0)
{
txt="Error";
}
else
{
var parents = docMenu.documentElement.selectNodes("ParentMenu");
if(parents != null && parents.length > 0)
{
for(var i=0;i<parents.length;++i)
{
if(parents[i].getAttribute("Active") == 0)
{
txt += "<table cellspacing=\"1\" class=\"grid fixed\"><tr><td class=\"category linkdisable\">≡ { " + parents[i].getAttribute("Name") + " } ≡</td></tr></table><table cellspacing=\"0\"><tr><td colspan=\"3\" style=\"height: 1px;\"><img alt=\"\" src=\"images/blank.gif\" /></td></tr></table>";
continue;
}
else
{
txt += "<table cellspacing=\"1\" class=\"grid fixed\"><tr><td class=\"category hand\" onclick=\"collapse('menu_"+ i +"')\">≡ { " + parents[i].getAttribute("Name") + " } ≡</td></tr><tr id=\"menu_" + i + "\"><td class=\"items\"><ul>";
}
var children = parents[i].childNodes;
if(children != null && children.length > 0)
{
for(var j=0;j<children.length;++j)
{
if(children[j].getAttribute("Active") == 0)
{
txt += "<li class=\"linkdisable\">" + children[j].getAttribute("Name") + "</li>";
}
else
{
txt += "<li><a href=\"" + children[j].getAttribute("Url") + "\">" + children[j].getAttribute("Name") + "</a></li>";
}
}
}
else
{
txt += "<li><a href=\"#\"></a></li>";
}
txt += "</ul></td></tr></table><table cellspacing=\"0\"><tr><td colspan=\"3\" style=\"height: 1px;\"><img alt=\"\" src=\"images/blank.gif\" /></td></tr></table>";
}
}
}
}
else
{
txt="<table cellspacing=\"1\" class=\"grid fixed\"><tr><td class=\"category hand\">Loading</td></tr></table>";
}
leftBar.innerHTML = txt;
}