52,797
社区成员
发帖
与我相关
我的任务
分享
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>动态菜单</title>
<style>
* {
margin: 0px;
padding: 0px;
border: 0px;
}
body {
text-align: center
}
#bar {
width: 200px;
font-family: Arial;
text-align: left;
margin: 0px auto;
}
#ullist {
margin: 0;
padding: 0;
list-style-type: none;
}
#ullist li {
border-bottom: 1px solid #ed9f9f;
}
#ullist li a {
display: block;
text-decoration: none;
padding: 5px 5px 5px 0.5em;
border-left: 12px solid #711515;
border-right: 1px solid #711515;
}
#ullist li a:link,#ullist li a:visited {
background-color: #c11136;
color: #ffffff;
}
#ullist li a:hover {
background-color: #990020;
color: #ffff00;
}
/* 子菜单CSS样式*/
#bar ul li ul {
list-style-type: none;
margin: 0px;
padding: 0px 0px 0px 0px;
}
#bar ul li ul li {
border-top: 1px solid #ed9f9f;
}
#bar ul li ul li a {
display: block;
padding: 3px 3px 3px 0.5em;
text-decoration: none;
border-left: 28px solid #a71f1f;
border-right: 1px solid #711515;
}
#bar ul li ul li a:link,#bar ul li ul li a:visited {
background-color: #e85070;
color: #FFFFFF;
}
#bar ul li ul li a:hover {
background-color: #c2425d;
color: #ffff00;
}
#bar ul li ul.myHide {
display: none;
}
#bar ul li ul.myShow {
display: block;
}
</style>
<script type="text/javascript">
function changes() {
var oSecondDiv = this.parentNode.getElementsByTagName("ul")[0];
if (oSecondDiv.className == "myHide") {
oSecondDiv.className = "myShow"
} else {
oSecondDiv.className = "myHide"
}
}
window.onload = function() {
var oUl = document.getElementById("ullist");
var aLi = oUl.childNodes;
var oA;
var i = 0;
for (i; i < aLi.length; i++) {
if (aLi[i].nodeType == 1) {
if (aLi[i].tagName == "LI"
&& aLi[i].getElementsByTagName("ul").length) {
// alert(aLi[i].innerHTML);
oA = aLi[i].getElementsByTagName("a")[0];
if (oA)
oA.onclick = changes;
}
}
}
}
</script>
</head>
<body>
<div id="bar">
<ul id="ullist">
<li>
<a href="#">主菜单1 </a>
</li>
<li>
<a href="#">主菜单2 </a>
<ul class="myHide">
<li>
<a href="#">子菜单1 </a>
</li>
<li>
<a href="#">子菜单2 </a>
</li>
</ul>
</li>
<li>
<a href="#">主菜单3 </a>
<ul class="myHide">
<li>
<a href="#">子菜单1 </a>
</li>
<li>
<a href="#">子菜单2 </a>
</li>
</ul>
</li>
<li>
<a href="#">主菜单4 </a>
<ul class="myHide">
<li>
<a href="#">子菜单1 </a>
</li>
<li>
<a href="#">子菜单2 </a>
</li>
</ul>
</li>
<li>
<a href="#">主菜单5 </a>
<ul class="myHide">
<li>
<a href="#">子菜单1 </a>
</li>
<li>
<a href="#">子菜单2 </a>
</li>
</ul>
</li>
<li>
<a href="#">主菜单6 </a>
</li>
</ul>
</div>
</body>
</html>
//兼容FF IE
var getElementsByName = function(tag, name){
var returns = document.getElementsByName(name);
if(returns.length > 0) return returns;
returns = new Array();
var e = document.getElementsByTagName(tag);
for(var i = 0; i < e.length; i++){
if(e[i].getAttribute("name") == name){
returns[returns.length] = e[i];
}
}
return returns;
}
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>动态菜单</title>
<style>
* {
margin: 0px;
padding: 0px;
border: 0px;
}
body {
text-align: center
}
#bar {
width: 200px;
font-family: Arial;
text-align: left;
margin: 0px auto;
}
#ullist {
margin: 0;
padding: 0;
list-style-type: none;
}
#ullist li {
border-bottom: 1px solid #ed9f9f;
}
#ullist li a {
display: block;
text-decoration: none;
padding: 5px 5px 5px 0.5em;
border-left: 12px solid #711515;
border-right: 1px solid #711515;
}
#ullist li a:link,#ullist li a:visited {
background-color: #c11136;
color: #ffffff;
}
#ullist li a:hover {
background-color: #990020;
color: #ffff00;
}
/* 子菜单CSS样式*/
#bar ul li ul {
list-style-type: none;
margin: 0px;
padding: 0px 0px 0px 0px;
}
#bar ul li ul li {
border-top: 1px solid #ed9f9f;
}
#bar ul li ul li a {
display: block;
padding: 3px 3px 3px 0.5em;
text-decoration: none;
border-left: 28px solid #a71f1f;
border-right: 1px solid #711515;
}
#bar ul li ul li a:link,#bar ul li ul li a:visited {
background-color: #e85070;
color: #FFFFFF;
}
#bar ul li ul li a:hover {
background-color: #c2425d;
color: #ffff00;
}
#bar ul li ul.myHide {
display: none;
}
#bar ul li ul.myShow {
display: block;
}
</style>
<script type="text/javascript">
function changes() {
var oSecondDiv = this.parentNode.getElementsByTagName("ul")[0];
if (oSecondDiv.className == "myHide") {
oSecondDiv.className = "myShow"
} else {
oSecondDiv.className = "myHide"
}
}
window.onload = function() {
var oUl = document.getElementById("ullist");
var aLi = oUl.childNodes;
alert("aLi.length:" + aLi.length); // IE7下值为6,在FF下值为13?
var oA;
var i = 0;
for (i; i < aLi.length; i++) {
if (aLi[i].nodeType == 1) {
alert(aLi[i].innerHTML); //
}
if (aLi[i].nodeType == 3) {
alert(aLi[i].nodeValue); //
}
if (aLi[i].tagName == "LI" && aLi[i].getElementsByTagName("ul").length) {
// alert(aLi[i].innerHTML);
oA = aLi[i].firstChild;
oA.onclick = changes;
}
}}
</script>
</head>
<body>
<div id="bar">
<ul id="ullist">文字内容测试,这里如果是空白,也是节点啊
<li>
<a href="#">主菜单1 </a>
</li>
<li>
<a href="#">主菜单2 </a>
<ul class="myHide">
<li>
<a href="#">子菜单1 </a>
</li>
<li>
<a href="#">子菜单2 </a>
</li>
</ul>
</li>
<li>
<a href="#">主菜单3 </a>
<ul class="myHide">
<li>
<a href="#">子菜单1 </a>
</li>
<li>
<a href="#">子菜单2 </a>
</li>
</ul>
</li>
<li>
<a href="#">主菜单4 </a>
<ul class="myHide">
<li>
<a href="#">子菜单1 </a>
</li>
<li>
<a href="#">子菜单2 </a>
</li>
</ul>
</li>
<li>
<a href="#">主菜单5 </a>
<ul class="myHide">
<li>
<a href="#">子菜单1 </a>
</li>
<li>
<a href="#">子菜单2 </a>
</li>
</ul>
</li>
<li>
<a href="#">主菜单6 </a>
</li>
</ul>
</div>
</body>
</html>
<ul class="myHide"><li> <a href="#">子菜单1 </a></li><li><a href="#">子菜单2 </a></li></ul>