Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JQuery插件中的this.selector-无法读取未定义的属性“top”_Javascript_Jquery_Jquery Plugins_This - Fatal编程技术网

Javascript JQuery插件中的this.selector-无法读取未定义的属性“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

我看到了很多关于这个错误的问题,没有一个对这种情况有很大帮助

我试图编写一个JQuery插件,但是我得到了一个错误UncaughtTypeError:无法读取未定义的属性'top'。这似乎与此有关。例如,这似乎很有效:

 $(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”。谢谢