1
$container.infinitescroll({
        navSelector  : "div.navigation",
        nextSelector : "div.next-page a:first",   
        itemSelector : "#posts-container div.post",
        bufferPx     : 80
        },
        function( newElements ) {
            var $newElems = $ ( newElements );
            $container.masonry( 'appended', $newElems );
        }
);

在实现infiniteScroll插件时,我在newElements不知道它到底是什么的情况下使用了它。在呈现的代码中,我newElements作为参数传入,但它没有在上面的任何地方声明。但是,该功能工作正常;新帖子元素存储在$newElems.

当我使用newElements时,我是否方便地将所有新添加的元素调用到 DOM 中?

4

2 回答 2

5

newelements在 JavaScript 中什么都不是。它是一个在加载新项目时传递给回调函数的变量。

您可能应该阅读 InfiniteScroll 的文档,其中清楚地详细说明了此功能:

function(arrayOfNewElems){

     // optional callback when new content is successfully loaded in.

     // keyword `this` will refer to the new DOM content that was just added.
     // as of 1.5, `this` matches the element you called the plugin on (e.g. #content)
     //                   all the new elements that were found are passed in as an array
}

您可以为回调参数指定任何名称,但它将包含新元素的数组。

于 2013-08-20T08:32:30.633 回答
2

首先,阅读这个插件的文档:http: //www.infinite-scroll.com/infinite-scroll-jquery-plugin/

如您所见,回调函数的注释描述:

function(arrayOfNewElems){
    // optional callback when new content is successfully loaded in.

    // keyword `this` will refer to the new DOM content that was just added.
    // as of 1.5, `this` matches the element you called the plugin on (e.g. #content)
    // all the new elements that were found are passed in as an array
}

这意味着,回调函数的 newElements 参数实际上包含(一个数组)所有新添加的元素。

newElements 之前不必声明的原因是它是一个函数参数。更多关于函数的信息可以在这里找到: http: //www.w3schools.com/js/js_functions.asp

于 2013-08-20T08:34:26.550 回答