採用JavaScript+XML實現具有樹形選單功能的論壇側邊導航欄
核心頁面程式碼:
(bbsMenu.htm)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> VsensBBS </TITLE>
<style>
body,td{font:12px verdana; height:50px;}
#treeBox{background-color:#fffffa;}
#treeBox .ec{margin:0 5 0 5;}
#treeBox .hasItems{font-weight:bold;height:20px;padding:3 6 0 6;margin:2px;cursor:hand;color:#555555;border:1px solid #fffffa;}
#treeBox .Items{height:20px;padding:3 6 0 6;margin:1px;cursor:hand;color:#555555;border:1px solid #fffffa;}
</style>
<script>
var HC = "color:#990000;border:1px solid #cccccc";
var SC = "background-color:#efefef;border:1px solid #cccccc;color:#000000;";
var IO = null;
function initTree(){
var rootn = document.all.menuXML.documentElement;
var sd = 0;
document.onselectstart = function(){return false;}
document.all.treeBox.appendChild(createTree(rootn,sd).childNodes[2]);
}
function createTree(thisn,sd){
var nodeObj = document.createElement("span");
var upobj = document.createElement("span");
with(upobj){
style.marginLeft = sd*10;
className = thisn.hasChildNodes()?"hasItems":"Items";
innerHTML = "<img src=image/expand.gif class=ec>" + thisn.getAttribute("text") +"";
onmousedown = function(){
if(event.button != 1) return;
if(this.getAttribute("cn")){
this.setAttribute("open",!this.getAttribute("open"));
this.cn.style.display = this.getAttribute("open")?"inline":"none";
this.all.tags("img")[0].src = this.getAttribute("open")?"image/expand.gif":"image/collapse.gif";
}
if(IO){
IO.runtimeStyle.cssText = "";
IO.setAttribute("selected",false);
}
IO = this;
this.setAttribute("selected",true);
this.runtimeStyle.cssText = SC;
}
onmouseover = function(){
if(this.getAttribute("selected"))return;
this.runtimeStyle.cssText = HC;
}
onmouseout = function(){
if(this.getAttribute("selected"))return;
this.runtimeStyle.cssText = "";
}
oncontextmenu = contextMenuHandle;
onclick = clickHandle;
}
if(thisn.getAttribute("treeId") != null){
upobj.setAttribute("treeId",thisn.getAttribute("treeId"));
}
if(thisn.getAttribute("href") != null){
upobj.setAttribute("href",thisn.getAttribute("href"));
}
if(thisn.getAttribute("target") != null){
upobj.setAttribute("target",thisn.getAttribute("target"));
}
nodeObj.appendChild(upobj);
nodeObj.insertAdjacentHTML("beforeEnd","<br>")
if(thisn.hasChildNodes()){
var i;
var nodes = thisn.childNodes;
var cn = document.createElement("span");
upobj.setAttribute("cn",cn);
if(thisn.getAttribute("open") != null){
upobj.setAttribute("open",(thisn.getAttribute("open")=="true"));
upobj.getAttribute("cn").style.display = upobj.getAttribute("open")?"inline":"none";
if( !upobj.getAttribute("open"))upobj.all.tags("img")[0].src ="image/collapse.gif";
}
for(i=0;i<nodes.length;cn.appendChild(createTree(nodes[i++],sd+1)));
nodeObj.appendChild(cn);
}
else{
upobj.all.tags("img")[0].src ="image/endnode.gif";
}
return nodeObj;
}
window.onload = initTree;
</script>
<script>
function contextMenuHandle(){
event.returnValue = false;
var treeId = this.getAttribute("treeId");
// your code here
}
function clickHandle(){
window.open(this.href,"mainFrame");
}
</script>
</HEAD>
<BODY>
<xml id=menuXML>
<?xml version="1.0" encoding="GB2312"?>
<DSTreeRoot text="華盛社群" open="true" href="http://" treeId="123">
<DSTree text="產品交流" open="false" href="" target="_self" treeId="12">
<DSTree text="產品交流子節點" href="http://www.baidu.com" target="_blank" treeId="4353" />
<DSTree text="產品交流子節點" href="http://www.jnunet.cn/assets" target="box" treeId="543543" />
<DSTree text="產品交流子節點" href="" target="box" treeId="543543" />
</DSTree>
<DSTree text="產品銷售" open="false" href="" treeId="213">
<DSTree text="產品銷售子節點" href="http://www.baidu.com" treeId="4353" />
<DSTree text="產品銷售子節點" href="" treeId="543543" />
</DSTree>
<DSTree text="渠道物流" open="false" href="" treeId="432">
<DSTree text="渠道物流子節點" href="" treeId="4353" />
<DSTree text="渠道物流子節點" href="" treeId="543543" />
</DSTree>
<DSTree text="市場推廣" open="false" href="" target="box" treeId="213">
<DSTree text="市場推廣子節點" href="" treeId="4353" />
<DSTree text="市場推廣子節點" href="" treeId="543543" />
</DSTree>
</DSTreeRoot>
</xml>
<table style="position:absolute;left:0;top:0;">
<tr>
<td id=treeBox style="width:250px;height:600px;;border:1px solid #cccccc;padding:45 3 3 5; background-image:url(image/bbsTitleBack.gif); background-position:top; background-repeat:repeat-x; padding-right:65px; " valign=top></td>
</tr>
</table>
</BODY>
</HTML>
其中,function clickHandle(){
window.open(this.href,"mainFrame");
}
為滑鼠點選某一節點後執行的動作。全部程式碼來源於我近期的一個專案,客戶要求論壇必須有側邊導航欄,我於是想到用框架來實現。"mainFrame"是主顯框架的ID及name屬性。讀者可靈活修改。
附上框架全部程式碼及圖片打包下載:
http://download.csdn.net/source/340556
相關文章
- TornadoFx實現側邊欄選單效果
- ElementUI側邊欄導航選單隱藏顯示問題UI
- C# 簡單反射實現winform左側樹形導航,右側切換內容C#反射ORM
- tpextbuilder- 左側樹形導航UI
- Bootstrap 4 實現導航欄右側對齊boot
- 利用transform skewX製作平行四邊形導航選單ORM
- 點選平滑下拉展開摺疊樹形導航選單
- 來,搞個側欄導航
- JavaScript實現HTML導航欄下拉選單[懸浮顯示]JavaScriptHTML
- Win10 隱藏左側邊側欄(導航視窗)中的資料夾Win10
- 使用Vue實現後臺管理系統的動態路由以及側邊欄選單Vue路由
- css3實現側邊滑動選單CSSS3
- CSS導航欄及下拉選單CSS
- 直播軟體搭建,通過Android DrawerLayout實現側邊欄功能Android
- jQuery簡單實用的響應式固定側邊欄外掛jQuery
- [BUG反饋]為什麼左側選單欄和導航欄無緣無故消失了
- 左側導航欄element -2024/11/27
- PbootCMS導航選單-導航選單的使用教程boot
- 2018.3.30 邊框應用與導航欄設定
- WPF實現樹形下拉選單框(TreeComboBox)
- 影片直播原始碼,預設展開側邊欄選單原始碼
- 純CSS打造淘寶導航選單欄CSS
- 導航欄點選選中
- JavaScript滑鼠懸浮展開側欄導航JavaScript
- 室內導航用什麼來實現?通過什麼可以實現導航功能?
- 垂直摺疊導航選單實現詳解
- 購物網站側欄商品分類導航網站
- Ubuntu 20.04 自動隱藏左側導航欄Ubuntu
- HTML歷理 ICbA的側欄選單HTML
- Vue遞迴元件實現樹形結構選單Vue遞迴元件
- Flutter drawer側邊欄Flutter
- Android 使用BottomNavigationView實現底部導航欄AndroidNavigationView
- 適合移動手機的jQuery多級側邊欄選單外掛jQuery
- Flutter 側滑欄及城市選擇UI的實現FlutterUI
- WPF/C#:實現導航功能C#
- CSS3實現側邊欄快速定位的隱藏和消失CSSS3
- Flutter仿閒魚底部導航欄實現Flutter
- jQuery實現吸頂動畫導航欄jQuery動畫