js兼容性问题求助

Yoking_Ma 2012-08-20 03:23:24
我对js只了解皮毛,刚接触,现遇到个问题:下面这段可在IE8以下运行,谷歌、火狐(貌似IE9也是)均无反应,不知道出在哪儿的问题?
(实现的功能类似与二级菜单,鼠标点击一级菜单会展开2级,再点会隐藏二级菜单)

部分代码:

html问题:

<ul class="catlist">
<li id='1' cla='cla' parentId='0' parentpath='0' depth='0' style='cursor:pointer;display: block;' onclick='javascript:hs(1,"0")' class=''>+CAT</li>
<li id='3' cla='cla' parentId='1' parentpath='0,1' depth='1' style='cursor:pointer;display:none;' onclick='javascript:hs(3,"0,1")' class=''><a href='prolist.asp?classId=3&parentPath=0,1'>-cat1</a></li>
<li id='4' cla='cla' parentId='1' parentpath='0,1' depth='1' style='cursor:pointer;display:none;' onclick='javascript:hs(4,"0,1")' class=''><a href='prolist.asp?classId=4&parentPath=0,1'>-cat2</a></li>
<li id='5' cla='cla' parentId='1' parentpath='0,1' depth='1' style='cursor:pointer;display:none;' onclick='javascript:hs(5,"0,1")' class=''><a href='prolist.asp?classId=5&parentPath=0,1'>-cat3</a></li>
<li id='18' cla='cla' parentId='1' parentpath='0,1' depth='1' style='cursor:pointer;display:none;' onclick='javascript:hs(18,"0,1")' class=''><a href='prolist.asp?classId=18&parentPath=0,1'>-cat4</a></li>
<li id='19' cla='cla' parentId='1' parentpath='0,1' depth='1' style='cursor:pointer;display:none;' onclick='javascript:hs(19,"0,1")' class=''><a href='prolist.asp?classId=19&parentPath=0,1'>-cat5</a></li>
</ul>


JS部分:

<script type='text/javascript'>
function hs(tId,parentpath)
{
var i;a=document.getElementsByTagName('li');
for (i=0;i<a.length;i++){
if(a[i].cla=='cla'){
if(a[i].depth=='0' || parentpath.indexOf(a[i].parentpath+',')>=0 || a[i].parentpath==parentpath || a[i].parentId==tId)
if(a[i].parentId==tId)
if(a[i].style.display=='block')
a[i].style.display='none';
else
a[i].style.display='block';
else
a[i].style.display='block';
else
a[i].style.display='none';
}
}
}</script>

...全文
183 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
花钱月下 2012-08-20
  • 打赏
  • 举报
回复
<html>
<head>



</head>
<body>
<ul class="catlist">
<li id='1' cla='cla' parentId='0' parentpath='0' depth='0' style='cursor:pointer;display: block;' onclick='javascript:hs(1,"0")' class=''>+CAT</li>
<li id='3' cla='cla' parentId='1' parentpath='0,1' depth='1' style='cursor:pointer;display:none;' onclick='javascript:hs(3,"0,1")' class=''><a

href='prolist.asp?classId=3&parentPath=0,1'>-cat1</a></li>
<li id='4' cla='cla' parentId='1' parentpath='0,1' depth='1' style='cursor:pointer;display:none;' onclick='javascript:hs(4,"0,1")' class=''><a

href='prolist.asp?classId=4&parentPath=0,1'>-cat2</a></li>
<li id='5' cla='cla' parentId='1' parentpath='0,1' depth='1' style='cursor:pointer;display:none;' onclick='javascript:hs(5,"0,1")' class=''><a

href='prolist.asp?classId=5&parentPath=0,1'>-cat3</a></li>
<li id='18' cla='cla' parentId='1' parentpath='0,1' depth='1' style='cursor:pointer;display:none;' onclick='javascript:hs(18,"0,1")' class=''><a

href='prolist.asp?classId=18&parentPath=0,1'>-cat4</a></li>
<li id='19' cla='cla' parentId='1' parentpath='0,1' depth='1' style='cursor:pointer;display:none;' onclick='javascript:hs(19,"0,1")' class=''><a

href='prolist.asp?classId=19&parentPath=0,1'>-cat5</a></li>
</ul>

</body>
</html>
<script type='text/javascript'>
function hs(tId,parentpath)
{
var i;a=document.getElementsByTagName('li');
for (i=0;i<a.length;i++){
if(document.getElementById(a[i].id).getAttribute("cla")=='cla'){
if(document.getElementById(a[i].id).setAttribute("depth")=='0' || parentpath.indexOf(document.getElementById(a[i].id).getAttribute("parentpath")

+',')>=0 || document.getElementById(a[i].id).getAttribute("parentpath")==parentpath || document.getElementById(a[i].id).getAttribute("parentId")==tId)
if(document.getElementById(a[i].id).getAttribute("parentId")==tId)
if(a[i].style.display=='block')
a[i].style.display='none';
else
a[i].style.display='block';
else
a[i].style.display='block';
else
a[i].style.display='none';
}
}
}</script>


哥们 不要使用那么多自定义属性 IE是支持 但是谷歌和火狐就不一定了 有自定义属性 要兼容的话 用getAttribute获取setAttribute设置
d945332077 2012-08-20
  • 打赏
  • 举报
回复
parentId这个???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????错 说错了不好意思 是parentId这个属性在哪里????????????????????????????????????????????????????????????????????????????????????????????????????

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧