日期:2014-05-18  浏览次数:20373 次

JAVASCRIPT创建导航树
一个简单的例子.代码如下:
//HTML文档
<html>
<head>
<title>Creating a Navigation Tree</title>
<style>
A{text-decoration:none;}
#productsmenu,#supportmenu,#contactmenu
{
display:none;
margin-left:2em;
}
</style>
</head>
<body>
<h1>Navigation Tree Example</h1>
<p>The navigation tree below allows you to expand and collapse items.</p>
<ul>
<li><a id="products" href="#">[+] Products</a>
  <ul id="productsmenu">
  <li><a href="">Product List</a></li>
  <li><a href="">Order Form</a></li>
  <li><a href="">Price List</a></li>
  </ul>
</li>
<li><a id="support" href="#">[+] Support</a>
  <ul id="supportmenu">
  <li><a href="">Support Forum</a></li>
  <li><a href="">Contact Support</a></li>
  </ul>
</li>
<li><a id="contact" href="#">[+] Contact</a>
  <ul id="contactmenu">
  <li><a href="">Service Department</a></li>
  <li><a href="">Sales Department</a></li>
  </ul>
</li>
</ul>
<script language="javascript" type="text/javascript" src="tree.js">
</script>
</body>
</html>


//tree.js
function Toggle(e)
{
if(!document.geElementById) return; //浏览器是否支持DOM获得HTML元素
if(!e)
  var e=window.event;
whichlink=(e.target)?e.targer.id:e.srcElement.id;
obj=document.getElementById(whichlink+"menu");
visible=(obj.style.display=="block");
key=document.getElementById(whichlink);
keyname=key.firstChild.nodeValue.substring(3);
if(visible)
{
obj.style.display="none";
key.firstChild.nodeValue="[+]"+keyname;
}
else
{
obj.style.display="block";
key.firstChild.nodeValue="[-]"+keyname;
}
}
//为列表元素添加事件
document.getElementById("products").onclick=Toggle;
document.getElementById("support").onclick=Toggle;
document.getElementById("contact").onclick=Toggle;


在浏览器里打开HTML文档后 ,点[+] Products不会展开里面的列表,各位大哥看下是怎么回事啊.

------解决方案--------------------
确实有问题~
------解决方案--------------------
Hi, you should like this:

<!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=gb2312" />
<title>js树形菜单</title>
<style type="text/css">
<!--
a {text-decoration: none;}
#productsmenu, #supportmenu, #contactmenu {
display: none;
margin-left: 2em;
}
-->
</style>

<script language="javascript" type="text/javascript">

function Toggle(whichlink) 

obj=document.getElementById(whichlink+"menu"); 

visible=(obj.style.display=="block"); 
key=document.getElementById(whichlink); 
keyname=key.firstChil