Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单Javascript“;“未定义”;问题_Javascript_Undefined - Fatal编程技术网

简单Javascript“;“未定义”;问题

简单Javascript“;“未定义”;问题,javascript,undefined,Javascript,Undefined,我正在迭代一组子节点,并检查它们是否在页面上实际可见,使用以下语句: if(child.offsetWidth == 0 || child.offsetHeight == 0 || child.style.visibility == 'hidden' || child.style.display == 'none'){ 子项在循环中定义,因此这不是问题 问题是元素可能没有定义style属性,因此javascript返回未定义的“child.style” 我如何做这样一个看似简单的if语

我正在迭代一组子节点,并检查它们是否在页面上实际可见,使用以下语句:

if(child.offsetWidth == 0 || child.offsetHeight == 0 || child.style.visibility == 'hidden'     || child.style.display == 'none'){
子项在循环中定义,因此这不是问题

问题是元素可能没有定义style属性,因此javascript返回未定义的“child.style”

我如何做这样一个看似简单的if语句,而不因为某些东西没有定义而停止呢

我试着这样做:

if(undefined !== child.style){ var addquery = "child.style.visibility == 'hidden' ||     child.style.display == 'none'"; }
if(child.offsetWidth == 0 || child.offsetHeight == 0 || addquery){
console.debug(child);
}

但是我认为addquery只是计算为true,不起作用。

如果
这个.child
未定义的
,那么
&(
之间的下一部分将不会被计算

if(child.offsetWidth == 0 || child.offsetHeight == 0 || typeof child.style != 'undefined' && (child.style.visibility == 'hidden' || child.style.display == 'none')){

由于
&
是一个,如果
child.style
的计算结果为
true
,则它将只计算
child.style.visibility==“hidden”| child.style.display==“none”
,这意味着
child.style
存在,因此可以访问。如果
child.style
的计算结果为
false
,其余的将被忽略
child.style
不会被访问,因此不会抛出任何错误。

不要将
未定义的
用作关键字,因为它在JavaScript中不存在

然而,由于每个变量最初都是未定义的,因此针对一个不存在的
未定义的
变量进行测试通常是可行的。然而:

var undefined = 5; // this could be anywhere in the code

// [...]

if(foo === undefined) // *not* the test you want to be checking

相反,请使用
typeof foo==“undefined”
。只有实际未定义的变量才会从typeof运算符返回字符串
“undefined”
,检查每个子项的
节点类型属性。如果子元素是一个元素,则保证它具有
style
属性,并且不需要检查它是否未定义。您的代码清楚地表明您只对元素感兴趣,所以您应该确保只处理元素

if ((child.nodeTye == 1) && [...all the other conditions...]) {...}

此外,检查元素的
样式
属性的
显示
可见性
属性不会告诉您该元素在页面上是否实际可见,由于
style
对象仅包含通过
style
属性或
style
属性在元素上明确设置的值。您需要检查
display
visibility
的计算值,您可以使用它(或者IE中元素的
currentStyle
属性)。

如果您不希望由于错误而停止代码(例如
x.style.display
with
x.style
未定义将触发异常),但是您的代码可以继续运行,只需使用
try catch

   try {
        ...child.style.visibility...
   } catch(e) {
        // do either nothing or set child.style for instance
   }

通过这种方式,代码不会停止,您有办法发现问题,也有办法解决问题。

使用“addquery”的方法绝对不起作用;不能将包含代码的字符串隐藏在变量中,然后将其用作代码片段。语言根本不支持这一点。哈,是的……有点意识到了这一点。我只是在挖,你知道的…在问问题之前先尝试一下!这里的答案很有帮助。谢谢不完全是<代码>未定义
始终存在,因为它被定义为全局对象的属性,值为
未定义
。如果
undefined
确实不存在,
If(“foo”==undefined){}
将抛出
ReferenceError
。从未声明过的变量与已声明但值
未定义的变量之间有很大区别。但是,可以正确地说,
undefined
可以分配给,因此不可靠
   try {
        ...child.style.visibility...
   } catch(e) {
        // do either nothing or set child.style for instance
   }