无限滚动新元素

发布于 2025-01-03 14:44:21 字数 1345 浏览 0 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霓裳挽歌倾城醉 2025-01-10 14:44:21
$(elem).infinitescroll(options, applyvote(arrayOfNewElems));

.infinitescroll 的第二个参数是回调函数。 applyvote(arrayOfNewElems) 不是一个函数,而是一个函数调用(因此它计算出调用该函数返回的任何内容,这似乎是未定义)。使用这个代替:

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

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

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

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 be undefined). Use this instead:

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

Now you're passing a function which inifinitescroll itself will call when new content is loaded, and it will pass the new elements created.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文