主頁 > 知識庫 > Ajax讀取XML實現(xiàn)動態(tài)下拉導(dǎo)航

Ajax讀取XML實現(xiàn)動態(tài)下拉導(dǎo)航

熱門標簽:銀行業(yè)務(wù) 服務(wù)器配置 科大訊飛語音識別系統(tǒng) 電子圍欄 團購網(wǎng)站 阿里云 Linux服務(wù)器 Mysql連接數(shù)設(shè)置
根據(jù)客戶的需要做一個產(chǎn)品的分類的導(dǎo)航菜單,以前使用ASP遞歸讀取的。速度慢,而且消耗大量服務(wù)器資源。干脆改成AJAX+XML。共享出來和大家交流。希望各位能幫忙改進。
產(chǎn)品分類的XML文件
復(fù)制代碼 代碼如下:

//id為自身id,pid為父級分類ID 
?xml version="1.0" encoding="UTF-8" ?> 
Proot> 
       Item id="1" pid="0">1321系列/Item> 
       Item id="2" pid="1">43223系列/Item> 
/Proot> 

js代碼
復(fù)制代碼 代碼如下:

var root;
//FireFox不支持selectNodes方法,在網(wǎng)上找到這段代碼解決了這個問題。兼容了IE和FireFox.
//創(chuàng)建selectNodes方法
if( document.implementation.hasFeature("XPath", "3.0") )
{
   // prototying the XMLDocument
   XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
   {
      if( !xNode ) { xNode = this; } 
      var oNSResolver = this.createNSResolver(this.documentElement)
      var aItems = this.evaluate(cXPathString, xNode, oNSResolver, 
                   XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
      var aResult = [];
      for( var i = 0; i  aItems.snapshotLength; i++)
      {
         aResult[i] =  aItems.snapshotItem(i);
      }
      return aResult;
   }

   // prototying the Element
   Element.prototype.selectNodes = function(cXPathString)
   {
      if(this.ownerDocument.selectNodes)
      {
         return this.ownerDocument.selectNodes(cXPathString, this);
      }
      else{throw "For XML Elements Only";}
   }
}





function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest) {
        oXmlHttp = new XMLHttpRequest();
    }

}


function CreateXMLDOM()
{
createXMLHttpRequest();
oXmlHttp.open( "GET", "XML路徑", false ) ;
oXmlHttp.send(null) ;
root = oXmlHttp.responseXML.documentElement;
}
CreateXMLDOM()

function funCreatePullMenu(passPid,ChildId)
{

var currentItems = root.selectNodes("http://Proot/Item[@pid = " + passPid + "]");
var iItems = currentItems.length;
var k=0;
if(iItems > 0)
{

  
  var pdiv = document.createElement("DIV");
  pdiv.id="piv" + passPid;
  pdiv.className = "piv" + ChildId;
  pdiv.name = "piv" + passPid;
  if(passPid>0)
  {
       pdiv.style.display="none";
    document.getElementById("div" + passPid).appendChild(pdiv);
  }
  else
  {
    document.getElementById("odiv").appendChild(pdiv);
  }
  for(var i = 0; i  iItems; i++)
  {
   var _id = currentItems[i].attributes[0].value;
   var newChild = document.createElement("DIV");
   newChild.id="div" + _id;
   newChild.className = "div" + ChildId;
   newChild.name = "div" + _id;

   var _v ;
   if(CheckPullMenu(_id))
   {
    _1= _id
    _v = "a href='javascript:showsubmenu(" + _1 + ")'> " +currentItems[i].firstChild.data + "/a>";
   }
   else
   {
    _v = "a href='ProductList.aspx?type=" + _id + "'>"+ currentItems[i].firstChild.data +"/a>";
   }

   newChild.innerHTML=_v;

   
   document.getElementById("piv" + passPid).appendChild(newChild);
   if(CheckPullMenu(_id))
   {
   funCreatePullMenu(_id,ChildId+1)
   }
  }
}
}


//檢測是否有下級
function CheckPullMenu(passPid)
{

var currentItems = root.selectNodes("http://Proot/Item[@pid = " + passPid + "]");
var iItems = currentItems.length;
if(iItems > 0)
{
  return true;
}
    else
{
  return false;
}

}

//顯示隱藏層
function showsubmenu(sid)
{
var whichEl = document.getElementById( "piv" +sid);
if (whichEl.style.display == "none")
  {
           whichEl.style.display="block";
  }
else
  {
     whichEl.style.display="none";
  }
}


使用方法:在網(wǎng)頁中加入"div id="odiv">/div>"。在body加入onload="funCreatePullMenu(0,0)"

最終效果:


可以在代碼中增加定義CSS。達到更好的效果。

原發(fā)于:http://www.23mo.com/blog/article.asp?id=16
使用方法:在網(wǎng)頁中加入"div id="odiv">/div>"。在body加入onload="funCreatePullMenu(0,0)"

最終效果:


可以在代碼中增加定義CSS。達到更好的效果。

原發(fā)于:http://www.23mo.com/blog/article.asp?id=16
您可能感興趣的文章:
  • java AJAX實現(xiàn)級聯(lián)下拉框
  • Ajax實現(xiàn)無刷新三聯(lián)動下拉框
  • ajax 自動完成下拉框 自動提示位置問題
  • ajax讀取數(shù)據(jù)庫內(nèi)容實現(xiàn)二級聯(lián)動下拉選擇菜單示例

標簽:江蘇 衡水 蚌埠 衢州 大理 萍鄉(xiāng) 棗莊 廣元

巨人網(wǎng)絡(luò)通訊聲明:本文標題《Ajax讀取XML實現(xiàn)動態(tài)下拉導(dǎo)航》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266