资料
表格
制度
合同
管理
职场
经营
创业
范文
总结
计划
作文
文章
美文
词句
教程
模板
图片
字体
图标
办公
人事
财务
生产
管理资源吧首页
>>>
教程
>>>
编程
>>>
JavaScript教程
>>>
javascript 静态树菜单实现代码
谁有兴趣的话可以改成动态加载数据,或者用jquery,代码肯定会少很多!我发现添加一些css,这棵树在静态页面还是大有用途的!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <style type="text/css"> .expand{ cursor:pointer; } .hide{ cursor:pointer; } </style> <img src="/_New/_cj/myeditor/sysimage/space.gif" class="ewebeditor__script" _ewebeditor_fake_tag="script" _ewebeditor_fake_value="%3Cscript%20type%3D%22text%2Fjavascript%22%3E%20%0D%0Afunction%20toggleChild(o)%20%0D%0A%7B%20%0D%0Avar%20cls%20%3D%20o.getAttribute(%22class%22)%3B%20%0D%0Aif%20(cls%20%3D%3D%20%22expand%22)%20%0D%0A%7B%20%0D%0Avar%20sb%20%3D%20o.nextSibling%3B%20%0D%0Aif%20(window.innerWidth)%20sb%20%3D%20sb.nextSibling%3B%20%0D%0Awhile%20(sb%20!%3D%20null%20%26%26%20sb.tagName.toLowerCase()%20%3D%3D%20'dd')%20%0D%0A%7B%20%0D%0Asb.style.display%20%3D%20%22none%22%3B%20%0D%0Asb%20%3D%20sb.nextSibling%3B%20%0D%0Aif%20(window.innerWidth)%20sb%20%3D%20sb.nextSibling%3B%20%0D%0A%7D%20%0D%0Ao.removeAttribute(%22class%22)%3B%20%0D%0Ao.setAttribute(%22class%22%2C%20%22hide%22)%3B%20%0D%0Aif%20(window.innerWidth)%20%2F%2Fff%20%0D%0Ao.textContent%20%3D%20%22%E7%94%B0%22%20%2B%20o.textContent.substring(1)%3B%20%0D%0Aelse%20%0D%0Ao.innerText%20%3D%20%22%E7%94%B0%22%20%2B%20o.innerText.substring(1)%3B%20%0D%0A%7D%20%0D%0Aelse%20%0D%0A%7B%20%0D%0Avar%20sb%20%3D%20o.nextSibling%3B%20%0D%0Aif%20(window.innerWidth)%20sb%20%3D%20sb.nextSibling%3B%20%0D%0Awhile%20(sb%20!%3D%20null%20%26%26%20sb.tagName.toLowerCase()%20%3D%3D%20'dd')%20%0D%0A%7B%20%0D%0Asb.style.display%20%3D%20%22%22%3B%20%0D%0Asb%20%3D%20sb.nextSibling%3B%20%0D%0Aif%20(window.innerWidth)%20sb%20%3D%20sb.nextSibling%3B%20%0D%0A%7D%20%0D%0Ao.removeAttribute(%22class%22)%3B%20%0D%0Ao.setAttribute(%22class%22%2C%20%22expand%22)%3B%20%0D%0Aif%20(window.innerWidth)%20%2F%2Fff%20%0D%0Ao.textContent%20%3D%20%22%E6%9B%B0%22%20%2B%20o.textContent.substring(1)%3B%20%0D%0Aelse%20%0D%0Ao.innerText%20%3D%20%22%E6%9B%B0%22%20%2B%20o.innerText.substring(1)%3B%20%0D%0A%7D%20%0D%0A%7D%20%0D%0Afunction%20maketree(obj)%20%0D%0A%7B%20%0D%0Avar%20dts%20%3D%20obj.getElementsByTagName('dt')%3B%20%0D%0Afor%20(var%20i%20%3D%200%3B%20i%20%3C%20dts.length%3B%20i%2B%2B)%20%0D%0A%7B%20%0D%0Adts%5Bi%5D.setAttribute(%22class%22%2C%20%22expand%22)%3B%20%0D%0Aif%20(window.innerWidth)%20%2F%2Fff%20%0D%0Adts%5Bi%5D.textContent%20%3D%20%22%E6%9B%B0%22%20%2B%20dts%5Bi%5D.textContent%3B%20%0D%0Aelse%20%0D%0Adts%5Bi%5D.innerText%20%3D%20%22%E6%9B%B0%22%20%2B%20dts%5Bi%5D.innerText%3B%20%0D%0Adts%5Bi%5D.onclick%20%3D%20function()%20%7B%20toggleChild(this)%3B%20%7D%3B%20%0D%0A%7D%20%0D%0A%7D%20%0D%0Awindow.onload%20%3D%20function()%20%0D%0A%7B%20%0D%0Amaketree(document.getElementById(%22tree%22))%3B%20%0D%0A%7D%3B%20%0D%0A%3C%2Fscript%3E"> </head> <body> <h3>定义列表也能变成一棵树:</h3> <dl id="tree"> <dt>菜单1</dt> <dd>1.1 aaa</dd> <dd>1.2 bbb</dd> <dd>1.3 ccc</dd> <dt>菜单2</dt> <dd>2.1 你好</dd> <dd><a href='http://www.baidu.com' _ewebeditor_ta_href="http%3A%2F%2Fwww.baidu.com">超链接</a></dd> <dt>菜单3</dt> <dd> <dl style="margin-top:2px; margin-bottom:2px;"> <dt>3.1</dt> <dd><a href="http://news.baidu.com" _ewebeditor_ta_href="http%3A%2F%2Fnews.baidu.com">百度新闻</a></dd> <dd><a href="http://news.sohu.com" _ewebeditor_ta_href="http%3A%2F%2Fnews.sohu.com">搜狐新闻</a></dd> <dt>3.2</dt> <dd><a href="http://news.baidu.com" _ewebeditor_ta_href="http%3A%2F%2Fnews.baidu.com">百度新闻</a></dd> <dd><a href="http://news.sohu.com" _ewebeditor_ta_href="http%3A%2F%2Fnews.sohu.com">搜狐新闻</a></dd> </dl> </dd> </dl> </body> </html>
[Ctrl+A 全选 注:
如需引入外部Js需刷新才能执行
]
教程首页
更多教程