我有以下代码来检查对象是否存在的项目列表。 出于测试目的,我只有一个对象sub_total_0
,但脚本继续循环,因为typeof
不能确定sub_total_1
未定义或不存在,并继续前进2,3,4,5,...JavaScript检查对象存在
var i = 0;
while (typeof document.getElementById("sub_total_" + i) != "undefined") {
var obj_sub_total = document.getElementById("sub_total_" + i);
if (obj_sub_total != "undefined") {
if (fr.order_method_id.value == 1) {
obj_sub_total.style.visibility = "visible";
} else {
obj_sub_total.style.visibility = "hidden";
}
}
i++;
}
它完美的工作,感谢您的帮助。 – 2015-03-31 11:38:27