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

newelementsJavaScript では何もありません。これは、新しいアイテムが読み込まれたときにコールバック関数に渡される変数です。

おそらく、この関数の詳細を明確に説明している 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 に答える