简体   繁体   English

未捕获ReferenceError,我的函数未定义

[英]Uncaught ReferenceError, my function is not defned

I'm creating a dyanamic scrolling div and it works fine on Jsfiddle as seen here --> http://jsfiddle.net/9zXL5/19/embedded/result/ yet on the browser I get: 我正在创建一个动态滚动div,它在Jsfiddle上工作正常,如此处所示-> http://jsfiddle.net/9zXL5/19/embedded/result/但在浏览器上却得到了:

Uncaught TypeError: Cannot set property 'onscroll' of null 未被捕获的TypeError:无法将属性'onscroll'设置为null

So then I added $(document).ready(function (){ to my code and got 因此,我在代码中添加了$(document).ready(function (){

Uncaught ReferenceError: yHandler is not defined 未捕获的ReferenceError:yHandler未定义

I'm not understanding why I'm getting these errors yet its flowing smoothly on jsfiddle. 我不明白为什么我会收到这些错误,却在jsfiddle上流畅地运行。 I'd really appreciate it if someone could tell me what I'm not understanding or missing. 如果有人能告诉我我不了解或缺少的内容,我将非常感激。 Code in question is below 有问题的代码如下

var movelist = document.getElementById('movelist');
    $(document).ready(function (){
    function  yHandler (){
        var contentHeight = movelist.scrollHeight;
        var yOffset = movelist.clientHeight;
        var y = yOffset + movelist.scrollTop;
        if(y >= contentHeight){
            movelist.innerHTML += '<div class ="newData">hey look at me</div>';
        }
    }
});
movelist.onscroll = yHandler;

Put

var movelist = document.getElementById('movelist');

and

movelist.onscroll = yHandler;

inside your document.ready call. 在您的document.ready调用中。

Or, get rid of the document.ready call (you don't seem to have any jQuery in there anyway) and put your code in a script block at the end of your document before the closing body tag. 或者,摆脱document.ready调用(无论如何似乎都没有jQuery),然后将代码放在文档末尾的脚本块中,位于结束body标记之前。

jsFiddle example jsFiddle示例

您需要在标签$(document).ready(function (){...});之外定义函数$(document).ready(function (){...});

It's working in your Fiddle because everything is in the same scope. 它正在您的小提琴中工作,因为所有内容都在同一范围内。 You are declaring movelist outside your $(document).ready . 您在$(document).ready之外声明了movelist Move both the var movielist = ... and movielist.onscroll = yHandler; 移动var movielist = ...movielist.onscroll = yHandler; inside your $(document).ready . $(document).ready

You can also move your yHandler function outside your $(document).ready . 您还可以将yHandler函数移至$(document).ready

You should move all your code to within the onload handler 您应该将所有代码移到onload处理程序中

$(document).ready(function (){
    var movelist = document.getElementById('movelist');
    function  yHandler (){  
        var contentHeight = movelist.scrollHeight;
        var yOffset = movelist.clientHeight;
        var y = yOffset + movelist.scrollTop;

        if(y >= contentHeight){
            movelist.innerHTML += '<div class ="newData">hey look at me</div>';
        }          
    }
    movelist.onscroll = yhandler
});

Now your yhandler is within the same scope as the place that is using it. 现在,您的yhandler与使用它的地方在同一范围内。 In your example, it was defined an as inner function, and you were trying to access it from outside the function. 在您的示例中,它被定义为内部函数,并且您试图从函数外部访问它。 When you define inner functions, they are like local variables, they can only be access by the function itself. 当定义内部函数时,它们就像局部变量一样,只能由函数本身访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM