javascript实现列表的点击展开折叠

时间:2022-04-15 15:58:07
 <script>
window.onload = function()
{
//要折叠的区域
var catalog = document.getElementById("div-id");
//如果全部列表都添加折叠使用document.getElementsByTagName("ul");
var childs = catalog.getElementsByTagName("ul");
for (var i = 0; i < childs.length; i++)
{
var liList = childs[i].childNodes;
for (var j = 0; j < liList.length; j++)
{
if (liList[j].tagName == "LI")
{
addEvent(liList[j]);
}
}
}
}
function addEvent(o)
{
var as = o.getElementsByTagName("a");
as[0].onclick = function(e)
{
var theEvent = window.event || e;
var link = theEvent.srcElement ? theEvent.srcElement : theEvent.target;
var uls = link.parentNode.childNodes;
for (var i = 0;i < uls.length;i++)
{
if (uls[i].tagName == "UL")
{
if (uls[i].style.display == "none")
{
uls[i].style.display = "block";
}
else
{
uls[i].style.display = "none";
}
}
}
}
}

以上是javascript简单的对列表实现点击展开折叠功能