从外部的另一个事件调用一个事件内部的函数

时间:2014-05-20 14:32:50

标签: javascript function scope

我一直在努力解决一个javascript问题,我似乎无法找到解决它的解决方案。

如何调用这样的事件中的函数:

$('a.buttoncomments').live( 'click', function () { 
    function getComments(productId,revisionId,commentId) { 
        // some stuff 
    }
});

来自另一个事件:

$("#container-comments").on('scroll', function() {
if($(this).scrollTop() + $(this).innerHeight()>=$(this)[0].scrollHeight && onScroll === false) {
            onScroll = true;
            nTcommentId = $(".commentId").last().val();
            // call the function here -> getComments(value,value,value);
            onScroll = false;
        }
});

当我这样做时,它告诉我:

  

未捕获的ReferenceError:未定义getComments

嗯,我认为这是为了让你们告诉我什么是我失踪所需的所有代码!

先谢谢!

1 个答案:

答案 0 :(得分:2)

在click事件之外定义getComments。目前它的范围仅限于该功能。