Javascript JQuery插件中的this.selector-无法读取未定义的属性“top”
我看到了很多关于这个错误的问题,没有一个对这种情况有很大帮助 我试图编写一个JQuery插件,但是我得到了一个错误UncaughtTypeError:无法读取未定义的属性'top'。这似乎与此有关。例如,这似乎很有效:Javascript JQuery插件中的this.selector-无法读取未定义的属性“top”,javascript,jquery,jquery-plugins,this,Javascript,Jquery,Jquery Plugins,This,我看到了很多关于这个错误的问题,没有一个对这种情况有很大帮助 我试图编写一个JQuery插件,但是我得到了一个错误UncaughtTypeError:无法读取未定义的属性'top'。这似乎与此有关。例如,这似乎很有效: $(window).scroll(function() { var offsetBot = window.innerHeight - (($("#textBox1").offset().top - $(window).scrollTop()) + $("#textBox
$(window).scroll(function() {
var offsetBot = window.innerHeight - (($("#textBox1").offset().top - $(window).scrollTop()) + $("#textBox1").height());
console.log(offsetBot);
});
但是在下面的函数中,我得到了错误
$.fn.offBottom= function() {
$(window).scroll(function() {
if (!this.length) {
console.log("no element selected")
return;
} else{
var offsetBot = window.innerHeight - (($(this.selector).offset().top - $(window).scrollTop()) + $(this.selector).height());
}});
};
$("#textBox1").offBottom();
});
我试过用这个,$这个,还有这个选择器,但都不走运。
谢谢您已将$this上下文放置在$window滚动功能中。这就是为什么要获取DOM的元素[undefined],因此无法获取它的top属性
在此之前,您可能需要初始化DOM元素,如下所示:
$.fn.offBottom = function() {
var oElement = $(this); // $(this) will refer to the DOM that called the [offBottom] property method
$(window).scroll(function() {
if (oElement.length) {
console.log("no element selected");
return false;
} else {
console.log(oElement.offset().top);
var offsetBot = window.innerHeight - ((oElement.offset().top - $(window).scrollTop()) + $(this.selector).height());
}});
};
$("#textBox1").offBottom();
希望这对你的情况有帮助哦,这很有帮助。之后,我还必须将“$this.selector.height”更改为“oElement.height”。谢谢