Javascript 为什么我会得到;未捕获类型错误:无法读取属性';车身';“未定义”的定义;?
我的JS文件很简单Javascript 为什么我会得到;未捕获类型错误:无法读取属性';车身';“未定义”的定义;?,javascript,jquery,dom,javascript-events,event-handling,Javascript,Jquery,Dom,Javascript Events,Event Handling,我的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 = $(th
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");
}
};
});
给我带来麻烦的行是var body=document.body//IE“怪癖”
。错误
未捕获的TypeError:无法读取未定义的属性“body”
每次滚动时都会打印到控制台。然而,当我在控制台中键入
document.body
时,元素显示在控制台中,而不是未定义的。我也尝试过移动窗口。onscroll
在j查询(函数($)
之外,但我得到了相同的错误。这是因为,但不会让您太困惑,不要使用“document”作为变量的名称,因为您可以使用CSS使某个变量的高度与其宽度相同(不知道宽度)即使是现在,我也会非常感激的。看到这个答案了吗