getElementById()正在中断循环

getElementById() breaking loop

本文关键字:中断 循环 getElementById      更新时间:2023-09-26

我有问题。。。这是交易。我有很多元素和一个按钮来隐藏它们,看起来像

类别1(例如id="1")
 nbsp;-line1(例如div,id="1-1"class="blue")
 nbsp;-line2(例如div,id="1-2"class="blue")
 nbsp;-line3(例如div,id="1-3"class="red")
 nbsp;-line4(例如div,id="1-4"class="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

有什么建议吗?

全局作用域中没有getElementByIdgetElementByIddocument上的一个方法,因此您试图将未定义的值作为函数调用,但失败了。你想要更像这样的东西:

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

还要注意,逻辑已固定为使用&&而不是||