2013-08-20 2 views
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"는 자바 스크립트에서 어떻게 작동합니까?

newElements을 사용할 때 새로 추가 된 모든 요소를 ​​DOM에 편리하게 호출하고 있습니까?

답변

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 
} 

당신은 콜백 매개 변수에 어떤 이름을 할당 할 수 있지만 새로운 요소의 배열을 포함합니다. 당신이 볼 수 있듯이 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에 매개 변수를 모두

+0

이 질문을했을 때보 다 JavaScript가 훨씬 익숙해 졌기 때문에 지금 얻었습니다. 'infinitescroll'이 새로운 내용을 적재하면 자동으로 새로운 내용을 선택적 콜백 함수 인 function (newElements) {...}'에 인수로 전달합니다. 'newElements'는 매개 변수 이름 일 뿐이므로'loadedElements','newContent' 등과 같은 것이 될 수 있습니다. –

2

먼저,이 플러그인의 설명서를 참조 실제로 새로 추가 된 모든 요소 (배열)를 포함합니다.

newElements를 전에 선언 할 필요가없는 이유는 함수 인수이기 때문입니다. 함수에 대한 자세한 내용은 여기에서 확인할 수 있습니다. http://www.w3schools.com/js/js_functions.asp

+0

왜 대답을 올리지 않았습니까? 또한 제발, 제발 대답을 참조하지 않는 W3Schools ... http://www.w3fools.com/ – BenM

+1

때문에 내가 쓴 때 아직 게시되지 않았기 때문에 :) –

관련 문제