简体   繁体   English

JQuery addclass不支持AJAX调用

[英]JQuery addclass not working on AJAX call

I have a website in which I use an AJAX pagination system. 我有一个网站,我使用AJAX分页系统。 I then added a JQUERY call to add a class to some list items on my document ready function. 然后我添加了一个JQUERY调用,在我的文档就绪函数中为一些列表项添加一个类。

$(document).ready(function(){
    $(".products ul li:nth-child(3n+3)").addClass('last');
}

This all works correctly when the page is initially loaded, but when the AJAX pagination is used to change the page the addclass call is not made again. 这在页面初始加载时都能正常工作,但是当使用AJAX分页来更改页面时,不会再次进行addclass调用。 I think this is because the AJAX call does not fire the document ready function, so I think I need to add the addclass call to my AJAX POST call. 我想这是因为AJAX调用没有触发文件就绪函数,所以我想我需要在我的AJAX POST调用中添加addclass调用。 I tried something like this, but can't get it to work. 我尝试过类似的东西,却无法让它发挥作用。

$.ajax({
    type: "POST",
    url: "php/load_data.php",
    data: dataToSend,
    success: function(msg) {
        $("#container").ajaxComplete(function(event, request, settings) {   
            $(".products ul li:nth-child(3n+3)").addClass('last');

            loading_hide();
            $("#container").html(msg);
        });
    }
});

Am I correct in trying to add the addclass call in the AJAX succes callback? 我是否正确尝试在AJAX succes回调中添加addclass调用? If so what needs to change? 如果是这样,需要改变什么? Or where does this call need to be placed? 或者这个电话需要放在哪里?

After you load in your new data: 加载新数据后:

$("#container").html(msg);

Then apply the class: 然后申请上课:

$(".products ul li:nth-child(3n+3)").addClass('last');

All in the success method, so the final product: 所有的成功方法,所以最终产品:

success: function(msg) {
    $("#container").html(msg);
    $(".products ul li:nth-child(3n+3)").addClass('last');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM