getElementById()正在中断循环
getElementById() breaking loop
我有问题。。。这是交易。我有很多元素和一个按钮来隐藏它们,看起来像
类别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
有什么建议吗?
全局作用域中没有getElementById
,getElementById
是document
上的一个方法,因此您试图将未定义的值作为函数调用,但失败了。你想要更像这样的东西:
var el = document.getElementById(cla);
if(el && el.style.display != 'block')
el.style.display = 'block';
还要注意,逻辑已固定为使用&&
而不是||
。
相关文章:
- 中断javascript循环
- 当使用return语句时,循环不会中断
- Javascript数组推送中断循环
- 为什么内部Javascript循环会阻止外部循环中断(即无限循环)
- Javascript代码中断循环
- 在循环顶部中断循环的显式条件
- 完成ajax调用数组的循环,而不是在某个调用出错时中断
- 在负数之前中断循环
- 中断循环函数
- 中断循环的 Web Worker
- 中断语句不会中断循环
- 基于不存在的元素中断循环
- 节点函数未中断循环
- 这个for循环如何决定何时中断循环
- 是Javascript中的break关键字,仅用于中断循环
- getElementById()正在中断循环
- 在 Javascript 中从开关大小写内部中断循环
- 每三次迭代后中断循环
- jQuery 使用回调在 AJAX 请求中中断循环
- js继续和中断循环