我正在尝试用一堆产品进行无限滚动。我想在接近页面底部时附加一个带有 id 产品的div。我正在尝试这个,但这不起作用
$(window).scroll(function() {
if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) {
$('#container').append(('#products'));
}
});
它只是输出产品这个词。
作为旁注,我怎样才能使 ID 中包含数字。 对于示例
$('#container').append(('#products[i]'));
其中 i 是一个上升的数字。
从 @Vilsad P P 分叉...您仍在添加元素句柄而不是元素中的内容...... 这个添加了实际内容:
$(window).scroll(function() {
if ($(window).scrollTop() >= $(document).height() -
$(window).height() - 10) {
$('#container').append($('#products').html());
}
});
$('#products').html()
是元素中的内容,$('#products')
只是对元素的引用
现在要按 id 追加,您可以这样做:
var count = 0;
$(window).scroll(function() {
if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) {
var elem = "#products" + count;
$('#container').append($(elem).html());
count++;
}
});
这样做是它需要一个变量,其中包含与实际元素匹配的字符串,如果该字符串是有效的选择器,例如:$("#products0")
,那么它将返回 true。 非常合法。
请尝试以下操作
$(window).scroll(function() {
if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) {
$('#container').append($('#products'));
}
});
你只是添加文本而不是实际元素。