2012-01-29 150 views
-2

我遇到了问题......这是交易。我有很多元素和一个按钮隐藏所有的,看起来像getElementById()打破循环

组别(例如ID =“1”)
    - 一号线(例如那DIV ID =“1-1”级= “蓝色 ”)
    - LINE2(例如那格,ID =“ 1-2" 类=” 蓝色 “)
    - line3中(例如那格,ID =” 1 -3“class =”red“)
    - line4(例如thats div,id =”1-4“c lass =“green”)

等等线有不同的类,这是一个很长的故事。事情是我需要隐藏“Category1”标题(div),如果没有可见的元素。没关系。 也是我需要在该类别中的任何行会再次出现,以显示它...

所以我有这个
(IDL =行类(联想庞大,包含蓝色,红色,绿色等),通过自定义功能getElementsByClassName方法()......这就是“秀行”功能

for (i=0;i<idl.length;i++) 
    { 
    idl[i].style.display = "block"; 
    cla = idl[i].id; 
    if (cla[1]='-') {cla = cla[0];} 
    else {cla = cla[0] + cla[1];}  //weird way to get Category id but works 
            //just cut off "-1" "-2" part of line IDs 
            // loop is doing it's job. 

    /* if (getElementById(cla) || getElementById(cla).style.display!="block") { 
     getElementById(cla).style.display = "block"; 
     } 
    */ 

    // now here if I use 3 lines above it stops. 1st loop and that's it. 
    // even after getElementById("12345"), after getElementById(everything here) 
    // and nothing happening if i put alert anything after 

有什么建议?

+0

如果它“停止”意味着有错误。您应该始终在测试JavaScript时打开JavaScript控制台(或Firebug或其他调试工具),否则您将浪费很多时间。 – Pointy 2012-01-29 19:48:27

+0

用jquery,它会很简单。 '$('#1')。children()。hide();' – 2012-01-29 19:48:39

+0

另外,Id不应以数字开头。 – Johny 2012-01-29 19:50:32

回答

1

有在全球范围内没有getElementByIdgetElementByIddocument的方法使你想调用一个未定义的值作为函数 失败。你想要更多的东西是这样的:

var el = document.getElementById(cla); 
if(el && el.style.display != 'block') 
    el.style.display = 'block'; 

还要注意的是逻辑已经被固定,使用&&而非||

+0

是的,我的不好。但它仍然在el = document.getElementById(cla);中断。 (第一行) – 2012-01-29 19:58:41

+1

@Ярослав:“破”是什么意思?错误控制台中的任何内容?你确定'cla'是正确的吗?你有没有尝试给你的'id'属性添加一些不是数字的东西? – 2012-01-29 20:08:05