js, 树状菜单隐藏显示

js写的不是很严谨~~~嘿嘿

?

<!DOCTYPE html>

<html>

<head>

<meta
charset="UTF-8">

<style>

#ul_tree
ul {

????display: none;

}

?

</style>

<title>树状菜单</title>

</head>

<body>

?

<ul
id="ul_tree">

<li><a
href="javascript:void(0)">a</a>

????<ul
class="ul-second">

????????<li><a
href="javascript:void(0)">aa</a>

????????????<ul>

????????????????<li><a
href="javascript:void(0)">aa</a>

????????????????<li><a
href="javascript:void(0)">aa</a>

????????????????<li><a
href="javascript:void(0)">aa</a>

????????????</ul>

????????</li>

????????<li><a
href="javascript:void(0)">ab</a>

????????????<ul>

????????????????<li><a
href="javascript:void(0)">bb</a>

????????????????<li><a
href="javascript:void(0)">bb</a>

????????????????<li><a
href="javascript:void(0)">bb</a>

????????????</ul>

????????</li>

????????<li><a
href="javascript:void(0)">ac</a></li>

????????<li><a
href="javascript:void(0)">ad</a></li>

????</ul>

</li>

<li><a
href="javascript:void(0)">b</a>

????<ul
class="ul-second">

????????<li><a
href="javascript:void(0)">ba</a></li>

????????<li><a
href="javascript:void(0)">bb</a></li>

????????<li><a
href="javascript:void(0)">bc</a></li>

????????<li><a
href="javascript:void(0)">bd</a></li>

????</ul>

</li>

<li><a
href="javascript:void(0)">c</a>

????<ul
class="ul-second">

????????<li><a
href="javascript:void(0)">ca</a></li>

????????<li><a
href="javascript:void(0)">cb</a></li>

????????<li><a
href="javascript:void(0)">cc</a></li>

????????<li><a
href="javascript:void(0)">cd</a></li>

????</ul>

</li>

<li><a
href="javascript:void(0)">d</a>

????<ul
class="ul-second">

????????<li><a
href="javascript:void(0)">da</a></li>

????????<li><a
href="javascript:void(0)">db</a></li>

????????<li><a
href="javascript:void(0)">dc</a></li>

????????<li><a
href="javascript:void(0)">dd</a></li>

????</ul>

</li>

</ul>

<script>

(function(){

????var links = document.getElementsByTagName(‘a‘);

????for(var i = 0; i < links.length; i++){

????????links[i].addEventListener(‘click‘, function(e){

????????????setDisplay(e.target);

????????????

????????});

????}

????

})();

?

function setDisplay(obj){

????var fatherLi = getParent(obj, ‘li‘);

????var childUl = getFirstLevelChild(fatherLi, ‘ul‘);

????if(childUl.length > 0){

????????//console.log(childUl[0].style.display);

????????

????????if(childUl[0].style.display == ‘‘){

????????????childUl[0].style.display = "none";

????????}

????????if(childUl[0].style.display == ‘block‘){

????????????childUl[0].style.display = "none";

????????}else {

????????????childUl[0].style.display = "block";

????????}

????????

????}

}

function getFirstLevelChild(obj, tagname){

????var children = obj.childNodes;

????var returns = new Array();

????for(var i = 0, j = 0; i < children.length; i++){

????????if(children[i].nodeName.toLowerCase() == tagname.toLowerCase()){

????????????returns[j] = children[i];

????????????j++;

????????}

????}

????

????return returns;

}

?

function getParent(obj, parentTag){

????//console.log(obj);

????//alert();

????var returns;

????var _parent = obj.parentNode;

????//console.log(_parent);

????//console.log(_parent.nodeName);

????if(_parent.nodeName.toLowerCase() == parentTag.toLowerCase()){

????????returns = _parent;

????}else{

????????returns = getParent(_parent, parentTag);

????}

????return returns;

}

?

?

</script>

</body>

</html>

时间: 2024-10-19 09:36:50

js, 树状菜单隐藏显示的相关文章

Java Swing编程之仿js树状折叠菜单

最近要完成一个需求:用swing做个树状菜单,含二级菜单,点击一级菜单展开二级菜单,且二级菜单数目超过预览视图会出现滚动条.由于swing研究的少,花了不少精力! 先看下测试效果图: 收起图: 展开图: 完整源码: 1 package com.xuwei.test2; 2 3 import java.awt.BorderLayout; 4 import java.awt.Color; 5 import java.awt.GridLayout; 6 import java.awt.event.Ac

PHP+jquery 树状菜单

本项目开发过程中涉及树状菜单,于是做如下分享菜单实现 不足之处请大牛指点并见谅(如图) 1:数据表涉及字段 `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '系统菜单ID', `pid` int(10) DEFAULT NULL COMMENT '父级ID', `name` varchar(200) DEFAULT NULL COMMENT '菜单名称', `params` varchar(200) DEFAULT NULL COMME

纯CSS打造可折叠树状菜单

1:Html代码 <li> <label for="subsubfolder1">下级</label> <input id="subsubfolder1" type="checkbox" /> <ol> <li class="file"><a>下级</a></li> <li> <label for=&q

LigerUI 树状列表折叠显示

http://blog.csdn.net/haojuntu/article/details/8626040 —————————————————————————————————————————————————————————————————————————— function InitGrid(arrButtons) { //加载 菜单管理数据 $.post("Ajax_Menu.aspx", { view: 'ManageMenu' }, function(data) { gridRi

前端树状菜单

因为突然间用到这个东西,但是也不清楚别人是怎么做的,就自己根据自己的想法来了,效果还不错. 先思路,后代码: 思路:两个ul嵌套,把外层的ul显示出来,里层的ul先用样式表隐藏,当点击的时候,当前li下的ul状态更改为显示,就可以了 当点击其他的时候,先把外层li下所有的ul样式全为隐藏,然后再显示当前下的ul,大体思路就是这样 代码: <html><head>    <meta charset="UTF-8">    <title>Do

javascript树状菜单

应用页面 <html> <head> <title>category tree</title> <link rel="StyleSheet" href="css/dtree.css" type="text/css" /> <script type="text/javascript" src="script/dtree.js"></

前端的小玩意.删除.折叠.最大化.拖拽布局.树状菜单

以上是整体布局 添加Remove方法 Array.prototype.indexOf = function (val) { for (var i = 0; i < this.length; i++) { if (this[i] == val) return i; } return -1; }; Array.prototype.remove = function (val) { var index = this.indexOf(val); if (index > -1) { this.splic

js笔记----(运动)分享 隐藏/显示

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <style> body, div { padding:0px

五中常用简洁后台导航菜单(一级导航,二级导航,手风琴式导航,左或右悬浮式导航,树状导航)

首先附上源码,可以下载! http://yunpan.cn/cd9ivPcL4ayQT (提取码:d8a5) 在建立导航菜单的时候,我们首先布局一个(" 头"+[左,右(自适应)]+”尾“)的HTML页面: 来看一下HTML页面的编写: <div id="container"> <div id="head"></div> <div id="mainContent"> <di