无限滚动新元素
我正在尝试为无限滚动中的每个新元素添加投票功能。我设法使投票功能正常工作,但这不适用于向下滚动页面时加载的新元素。
Pastebin URL:http://pastebin.com/0eNYDXrm
我在下面附上了我的代码。任何帮助或建议将不胜感激...非常感谢!
<script type="text/javascript">
$('.protected-post-form').center();
$('#content').infinitescroll({
debug: false,
loading: {},
state: {
currPage: "1"
},
nextSelector: "div.navigation a:first",
navSelector: "div.navigation",
contentSelector: "#content",
itemSelector: "#content div.post",
pathParse: ["<?php echo $_SERVER["HTTP_HOST "] . $_SERVER["REQUEST_URI "] ?>page/", "/"]
}, function() {
window.setTimeout(infinite_scroll_callback(), 1);
});
function applyvote(elements) {
$(elements).each(
$(".vote a").click(
function() {
var some = jQuery(this);
var thepost = jQuery(this).attr("post");
var theuser = jQuery(this).attr("user");
jQuery.post("<?php bloginfo('template_url'); ?>/vote.php", {
user: theuser,
post: thepost
}, function(data) {
var votebox = ".vote" + thepost + " span";
jQuery(votebox).text(data);
});
});
});
}
$(elem).infinitescroll(options, applyvote(arrayOfNewElems));
});
</script>
I'm trying to add a voting function to each new element within the Infinite Scroll. I managed to get the voting functions working but this doesn't work with new elements loaded when scrolling down the page.
Pastebin URL: http://pastebin.com/0eNYDXrm
I've attached my code below. Any help or advice would be appreciated...Many Thanks!
<script type="text/javascript">
$('.protected-post-form').center();
$('#content').infinitescroll({
debug: false,
loading: {},
state: {
currPage: "1"
},
nextSelector: "div.navigation a:first",
navSelector: "div.navigation",
contentSelector: "#content",
itemSelector: "#content div.post",
pathParse: ["<?php echo $_SERVER["HTTP_HOST "] . $_SERVER["REQUEST_URI "] ?>page/", "/"]
}, function() {
window.setTimeout(infinite_scroll_callback(), 1);
});
function applyvote(elements) {
$(elements).each(
$(".vote a").click(
function() {
var some = jQuery(this);
var thepost = jQuery(this).attr("post");
var theuser = jQuery(this).attr("user");
jQuery.post("<?php bloginfo('template_url'); ?>/vote.php", {
user: theuser,
post: thepost
}, function(data) {
var votebox = ".vote" + thepost + " span";
jQuery(votebox).text(data);
});
});
});
}
$(elem).infinitescroll(options, applyvote(arrayOfNewElems));
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
.infinitescroll
的第二个参数是回调函数。applyvote(arrayOfNewElems)
不是一个函数,而是一个函数调用(因此它计算出调用该函数返回的任何内容,这似乎是未定义
)。使用这个代替:现在您正在传递一个函数,当加载新内容时 inifinitescroll 本身将调用该函数,并且它将传递创建的新元素。
The second argument to
.infinitescroll
is a callback function.applyvote(arrayOfNewElems)
is a not a function but a function call (so it evaluates to whatever is returned by your calling the function, which seems to beundefined
). Use this instead:Now you're passing a function which inifinitescroll itself will call when new content is loaded, and it will pass the new elements created.