简体   繁体   English

为什么我会收到“未捕获的类型错误:无法读取未定义的属性 'body'”?

[英]Why am I getting "Uncaught TypeError: Cannot read property 'body' of undefined"?

My JS file is simply我的 JS 文件很简单

jQuery(function ( $ ) {
    // make elements with class 'same-height-as-width' have the self-explanatory property
    $(window).resize(function ( ) {
        $('.same-height-as-width').each( function ( ) {
            var thisElement = $(this);
            thisElement.height(thisElement.width());        
        });
    });

    window.onscroll = function () {

        var body = document.body; //IE 'quirks'
        var document = document.documentElement; //IE with doctype
        document = (document.clientHeight) ? document : body;

        if (document.scrollTop == 0) {
            alert("top");
        }
    };

});

and the line that is causing me trouble is var body = document.body; //IE 'quirks'给我带来麻烦的那一行是var body = document.body; //IE 'quirks' var body = document.body; //IE 'quirks' . var body = document.body; //IE 'quirks' . The error错误

Uncaught TypeError: Cannot read property 'body' of undefined未捕获的类型错误:无法读取未定义的属性“正文”

is printed to the console every time I scroll.每次滚动时都会打印到控制台。 And yet, when I type document.body into the console, the element shows up in the console, is not undefined .然而,当我在控制台中键入document.body时,该元素显示在控制台中,不是undefined I've also tried moving the window.onscroll outside the j Query(function ( $ ) but I get the same error.我也尝试将window.onscroll移到 j Query(function ( $ )之外,但我得到了同样的错误。

这是因为JavaScript 变量提升,但不要让您感到困惑,不要使用“文档”作为变量的名称

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会收到Uncaught TypeError:无法在分配时读取未定义的属性“ 0”? - why am I getting Uncaught TypeError: Cannot read property '0' of undefined on assign? 我为什么会收到错误:Uncaught TypeError:无法读取未定义的属性“ left” - Why am I getting the error: Uncaught TypeError: Cannot read property 'left' of undefined 为什么会出现Uncaught TypeError:无法读取未定义的属性'displayQuestion'? - Why am i getting Uncaught TypeError: Cannot read property 'displayQuestion' of undefined? 在 Firebase 中,我收到 Uncaught TypeError: Cannot read property 'initializeApp' of undefined,但不确定为什么未定义“firebase”? - In Firebase I am getting an Uncaught TypeError: Cannot read property 'initializeApp' of undefined, but not sure why 'firebase' is not defined? 为什么我得到 Uncaught (in promise) TypeError: Cannot read property 'type' of undefined? - Why am I getting Uncaught (in promise) TypeError: Cannot read property 'type' of undefined? 为什么我收到此错误:“未捕获的TypeError:无法读取未定义的属性'标题'”? - Why am i getting this error: “Uncaught TypeError: Cannot read property 'Title' of undefined”? 为什么我收到“Select.js:1555 Uncaught TypeError:无法读取未定义的属性‘isSelectOptGroup’” - Why I am getting "Select.js:1555 Uncaught TypeError: Cannot read property 'isSelectOptGroup' of undefined" 为什么我收到此错误:未捕获的TypeError:无法读取undefined的属性'john' - Why am I getting this error: Uncaught TypeError: cannot read property 'john' of undefined 我为什么会收到TypeError:无法读取未定义的属性“现在” - Why am I getting TypeError: Cannot read property 'now' of undefined 为什么我收到“ TypeError:无法读取未定义的属性” length” - Why am I getting 'TypeError: Cannot read property 'length' of undefined'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM