【问题标题】:jQuery click won't work with new infinite scroll elementsjQuery click 不适用于新的无限滚动元素
【发布时间】:2014-03-17 04:05:41
【问题描述】:

在我的页面上,我有一个包含项目的列表,您可以单击“查看更多”按钮,该按钮会显示有关此主题的更多信息。这个点击功能在另一个页面上的 jQuery 中。我在此页面上实现了无限滚动,但现在“查看更多”按钮不适用于新元素,仅适用于第一个元素。

仅供参考:我没有编写此应用程序的代码,我的任务只是添加无限滚动。

我已经在网上搜索了有关此问题的信息,并且我已经阅读了几次这可能是因为新元素未初始化或其他原因。但我从来没有找到如何解决这个问题。

这里是无限滚动的代码:

varreachedEnd = false;

$(window).scroll(function() {
    if ($(window).scrollTop() == $(document).height() - $(window).height()) {
            lastPostFunc();
    }
});

function lastPostFunc() {

    var trs = $('.sresult-row'); /*get the number of trs*/
    var count = trs.length; /*this will work as the offset*/

    /*Restricting the request if the end is reached.*/
    if (reachedEnd == false) {
        $.ajax({
            url: "http://localhost:8080/jingjobs/index.php/search/ajax_searchJob/" + count,
            async: false,
            dataType: "html",
            success: function(data) {
                if (data != "End")
                    $('.result-bd').append(data);
                else
                    reachedEnd = true;
            }
        });
    }
}

“查看更多”点击功能代码:

$('.click-job-viewmore').click(function(e) {

    var abtn = $(this).parents('.sresult-row').find('.job-btn-submit');
    var abtn_submitted = $(this).parents('.sresult-row').find('.job-btn-submitted');
    var requestinterviewbtn = $(this).parents('.sresult-row').find('.jobseeker_request_interview');

    var oDom = $(this).parents('.sresult-row').find('.sresult-par2');
    var aMark = $(this).parents('.sresult-row').find('.job-mark');
    var aViewMore = $(this).find('.job-viewmore');
    var aEdit = $(this).find('.job-edit');

    if (oDom.css('display') == 'none') {

        if (window.location.href.indexOf('search/searchJobseeker') > 0) {
            $.post(site_url + 'user/updateVisitNum',
                    {uid: aViewMore.attr('alt')},
            function(result) {
            });

        }

        aMark.addClass('job-mark2').removeClass('job-mark1');

        oDom.slideDown();

        abtn.css({display: 'block'}).show();
        abtn_submitted.css({display: 'block'}).show();

        requestinterviewbtn.css({display: 'block'}).show();

        aViewMore.html("View Less");
        aViewMore.css({
            'color': '#674092'
        });
    } else {
        oDom.slideUp();

        abtn.hide();
        abtn_submitted.hide();

        requestinterviewbtn.hide();

        aMark.addClass('job-mark1').removeClass('job-mark2');

        aViewMore.html("View More");
        aViewMore.css({
            'color': '#ea6e3b'
        });
    }

    e.stopPropagation();

    e.preventDefault();
});

【问题讨论】:

    标签: jquery onclick infinite-scroll


    【解决方案1】:

    试试

    $(document).on("click",".click-job-viewmore",function(e) {});
    

    因为您应该将delegates 用于动态创建的对象。它将帮助您为要创建的未来元素附加事件。

    【讨论】:

    • 这正是我所需要的。我已经读过一些关于代表的东西,但我不知道什么或如何,所以感谢您的帮助。
    • 还有没有办法为 jQuery .find("elementname"); 做类似的事情?因为此页面中还使用了某些类型的 fxuitab,它们不再起作用了。
    • 如果是静态元素,可以使用 find() 方法。但在动态元素的情况下,需要委托
    • 它们在无限加载器中也是动态的。但是他们没有点击功能或其他东西,但他们被.find()搜索。那么我应该如何在这里使用这个代表呢?
    【解决方案2】:
    Attach the click event to the document 
    
    $(document).on('click','element' function(){}); 
    

    【讨论】:

      猜你喜欢
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 2014-11-18
      • 2015-09-01
      • 1970-01-01
      相关资源
      最近更新 更多