简体   繁体   English

jQuery对象返回未定义

[英]Jquery object returns undefined

So, i have a function that is called via a selector 所以,我有一个通过选择器调用的函数

$(window).scroll(function() {
    console.log($("#content div").inview());
});

However it returns undefined. 但是,它返回未定义。 When I return $elems it shows me all correct elements, but when i return $el it gives undefined. 当我返回$ elems时,它将显示所有正确的元素,但是当我返回$ el时,它将给出未定义的信息。

$.fn.inview = function() {

    var $elems = this;

    function getViewportHeight() {
        /*do tsoomething that works*/
        return height;
    }

    var vpH = getViewportHeight(),
    scrolltop = (document.documentElement.scrollTop ?
        document.documentElement.scrollTop :
        document.body.scrollTop);

    // return $elems.toArray();
    $elems.each(function () {
        var $el = $(this)
        height = $el.height() - (vpH / 1.5),
        inview = $el.data('inview') || false,
        topofscreen = scrolltop,
        botofscreen = topofscreen + vpH,
        topofelement= $el.offset().top;

        return $el;


};

inview doesn't have a return statement, so it always returns undefined inview没有return语句,因此它总是返回undefined

When you return $el; 当您return $el; you are doing it inside the anonymous function you pass to $elems.each 您正在传递给$elems.each的匿名函数中进行$elems.each

If you want to return something from inview then you need a return statement in that function. 如果要从inview返回某些内容,则需要在该函数中使用return语句。 Possibly you want to create an array just before you call each , push $el onto the array each time you go around the loop and then return the array. 可能您想在调用each之前创建一个数组,每次绕过循环然后将$el推入数组,然后返回该数组。

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

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