首页 > 解决方案 > .offset() 的值未定义

问题描述

我写了下面的函数

function test(use) {
  var x = jQuery(window).scrollTop();
  var y = x + jQuery(window).height();
  console.log(typeof(jQuery(use)));
  console.log(typeof(jQuery(use).offset()));
  var z = jQuery(use).offset().top;
  return ((y > z) && (z >= x - 100));
}

现在的问题是,每当调用此函数时,它都会给我一个错误,指出无法读取未定义的属性 top()。因此,问题在于变量 z。当我console.log()对第一个语句使用语句时,即console.log(typeof (jQuery(use)));我将值作为对象,但对于第二个语句,即console.log(typeof (jQuery(use).offset())); 我将值作为未定义。

谢谢

标签: javascriptjquery

解决方案


推荐阅读