jQuery从DOM中删除一个以上的元素

时间:2013-02-13 10:45:03

标签: jquery

在这篇Jquery中,我在显示早期reg_user_table的同一个地方显示了active_user_table。所以首先我要使reg_user_table显示为none,然后我将active_user_display设置为block。之后我发布getJSON,工作正常。问题是当表附加表时以及我想在所有表上附加类active_table时,我可以计算所有表并只允许其中一个表被删除或从DOM中删除或分离其他表。我无法这样做,类active_table应用于除最后一个之外的所有类。最近添加的没有显示类active_table。

 $('#reg_user_table').hide();
 if($('#active_user_table').is(':hidden')){   
     $('#active_user_table').css("display","block");
     $.getJSON("/siteadmin/active_user_count", function(response_data)
     { 
         var data = [response_data.today,response_data.Sevendays,response_data.Month];
         var caption = ['Active Today','Active Past 7 Days','Active This Month'];
         var $table = $('<table/>'); 

         for(var i = 0; i<3; i++) { 
             $table.append('<tr><td><h3>'+caption[i]+':'+'</h3></td><td><h3>'+ data[i] + '</h3></td></tr>');
         } 
         $('#active_user_table').append($table);
     });

     $('#active_user_table').children().addClass("active_table");
     console.log("Hello World!" + $('.active_table').length); 
}

1 个答案:

答案 0 :(得分:1)

欢迎来到异步世界。在最后两行之后执行$.getJSON。 把它们放在sucsess回调中

$.getJSON("/siteadmin/active_user_count", function (response_data) {
    //...
    $('#active_user_table').append($table);

    // put it here....  
    $('#active_user_table').children().addClass("active_table");
    console.log("Hello World!" + $('.active_table').length);
});