“新元素”在Javascript中是如何工作的?
在实现infiniteScroll插件时,我使用了新元素,但实际上并不知道它到底是什么。在给出的代码中,我将新元素作为参数传入,但上面没有声明它。但是,该函数运行良好;新的posts元素存储在$newElems中 当我使用新元素时,我是否方便地将所有新添加的元素都调用到DOM中?新元素在JavaScript中不是任何东西。它是一个变量,在加载新项时传递给回调函数 您可能应该仔细阅读InfiniteScroll的文档,其中清楚地详细介绍了此功能:“新元素”在Javascript中是如何工作的?,javascript,jquery,Javascript,Jquery,在实现infiniteScroll插件时,我使用了新元素,但实际上并不知道它到底是什么。在给出的代码中,我将新元素作为参数传入,但上面没有声明它。但是,该函数运行良好;新的posts元素存储在$newElems中 当我使用新元素时,我是否方便地将所有新添加的元素都调用到DOM中?新元素在JavaScript中不是任何东西。它是一个变量,在加载新项时传递给回调函数 您可能应该仔细阅读InfiniteScroll的文档,其中清楚地详细介绍了此功能: $container.infinitescroll
$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 );
}
);
您可以为回调参数指定任何名称,但它将包含新元素数组。新元素在JavaScript中不是任何东西。它是一个变量,在加载新项时传递给回调函数
您可能应该仔细阅读InfiniteScroll的文档,其中清楚地详细介绍了此功能:
$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 );
}
);
您可以为回调参数指定任何名称,但它将包含新元素数组。首先,请阅读此插件的文档: 如您所见,回调函数上的注释描述:
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参数实际上包含所有新添加元素的数组
新元素之前不必声明的原因是它是一个函数参数。有关函数的更多信息可在此处找到:首先,请阅读此插件的文档: 如您所见,回调函数上的注释描述:
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参数实际上包含所有新添加元素的数组
新元素之前不必声明的原因是它是一个函数参数。有关函数的更多信息,请参见此处:为什么不更新已发布的答案?另外,请不要在你的答案中提到学校。。。因为当我写我的答案时,它还没有发布:为什么不把已经发布的答案更新?另外,请不要在你的答案中提到学校。。。因为在我写我的问题时,它还没有被发布:因为我对JavaScript的熟悉程度比我问这个问题时要高得多,所以我现在明白了。当infinitescroll加载新内容时,它会自动将新内容作为参数传递给可选回调函数functionnewElements{…}。因为新元素只是一个参数名,它可以是任何东西,比如loadedElements、newContent等等。因为我对JavaScript的熟悉程度比我问这个问题时要高,所以我现在明白了。当infinitescroll加载新内容时,它会自动将新内容作为参数传递给可选回调函数functionnewElements{…}。因为newElements只是一个参数名,所以它可以是loadeelements、newContent等任何东西。