[英]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.