[英]javascript check object exists
I have following code to check the object whether exists for item listing. 我有以下代码来检查对象是否存在项目列表。 For testing purpose, i only have one object
sub_total_0
, however the script keep looping because typeof
cannot determine sub_total_1
is undefined or not exists, and keep going 2,3,4,5,... 出于测试目的,我只有一个对象
sub_total_0
,但是脚本保持循环,因为typeof
无法确定sub_total_1
是未定义的还是不存在,并继续2,3,4,5,...
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++;
}
You have 你有
typeof document.getElementById("sub_total_" + i) != "undefined"
and 和
if (obj_sub_total != "undefined") {
getElementById
returns either null
or an HTML element. getElementById
返回null
或HTML元素。 Neither of these are the string "undefined"
and the type of each of this will be "object"
. 这些都不是字符串
"undefined"
并且每个字符串的类型都是"object"
。 Thus your conditions don't make sense. 因此,你的条件没有意义。
You test for truthfulness instead. 你测试的是真实性。 An HTML element will always be true and
null
will always be false. HTML元素始终为true,
null
始终为false。
while (document.getElementById("sub_total_" + i)) {
and 和
if (obj_sub_total) {
getElementById() return null if element is not found, and the type of null
is object that is why your condition is not working. 如果找不到元素,则getElementById()返回null,并且
null
的类型是对象,这就是您的条件不起作用的原因。
You can just check whether it is a truthy value and since the while() loop validates the object there is no need for the if
condition 你可以检查它是否是一个真值,并且因为while()循环验证了对象,所以不需要
if
条件
var i = 0,
obj_sub_total;
while (obj_sub_total = document.getElementById("sub_total_" + i)) {
console.log(obj_sub_total)
if (fr.order_method_id.value == 1) {
obj_sub_total.style.visibility = "visble";
} else {
obj_sub_total.style.visibility = "hidden";
}
i++;
}
Your check doesn't work because using typeof
on getElementById
's return value will always give you "object"
, because it returns null
if it can't find it, and typeof null
is "object"
. 您的检查不起作用,因为在
getElementById
的返回值上使用typeof
将始终为您提供"object"
,因为如果找不到它则返回null
,而typeof null
是"object"
。
Just check the return value directly: If there's an element, it's a "truthy" value (one that coerces to true
when used as a condition); 只需直接检查返回值:如果有一个元素,则它是一个“真实”值(当用作条件时强制为
true
); if there isn't one, null
is a "falsey" value (coerces to false
). 如果没有,则
null
为“falsey”值(强制为false
)。
So: 所以:
while (document.getElementById("sub_total_" + i)) {
You also don't need to look it up twice, which is what you're currently doing; 你也不需要两次查找,这就是你现在正在做的事情; instead:
代替:
var obj_sub_total;
while ((obj_sub_total = document.getElementById("sub_total_" + i)) != null) {
(You don't technically need the != null
there, but without it it looks a bit like you've accidentally used =
where you wanted ==
.) (你在技术上不需要
!= null
,但没有它看起来有点像你不小心使用=
你想要的地方==
。)
Another alternative would be to use a class and querySelectorAll
: 另一种方法是使用类和
querySelectorAll
:
var list = document.querySelectorAll(".sub_total");
Then loop while i
is < list.length
, eg: 然后循环,而
i
是< list.length
,例如:
var list = document.querySelectorAll(".sub_total");
for (i = 0; i < list.length; ++i) {
var obj_sub_total = list[i];
// ...
}
Or you could do that even when using id
s: 或者即使使用
id
s你也可以这样做:
var list = document.querySelectorAll("[id^=sub_total]");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.