无限滚动新元素

时间:2012-02-07 21:36:40

标签: jquery infinite-scroll

我正在尝试为Infinite Scroll中的每个新元素添加一个投票功能。我设法让投票功能正常工作,但这不适用于向下滚动页面时加载的新元素。

Pastebin网址: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>

1 个答案:

答案 0 :(得分:0)

$(elem).infinitescroll(options, applyvote(arrayOfNewElems));

.infinitescroll的第二个参数是回调函数。 applyvote(arrayOfNewElems)不是函数而是函数调用(因此它会计算调用函数返回的任何函数,这似乎是undefined)。请改用:

$(elem).infinitescroll(options, applyvote);

现在你正在传递一个函数,当加载新内容时,inifinitescroll本身会调用它,它将传递创建的新元素。