jQuery的循环内克隆?

| 我有一个关于循环内克隆的问题,实际上是两个问题,我只是想找到解决问题的最佳方法,下面是我的代码。
$.each(data.customers,function(key, value){
    $(\'.customer\').find(\'label\').eq(0).text( value.name );
    $(\'.customer\').eq(0).clone().appendTo(\'#customers\');
});
data.customers是json格式,因此我无法在其上执行.length。我的问题是,无论如何,jquery总是将克隆元素附加在#customers的末尾。如果它们是json集合中的另一个,则只需要追加。我能想到的唯一方法是将数组的计数返回为json并检查键是否等于它,这似乎很荒谬。处理循环时,克隆元素的最佳方法是什么? 谁能帮忙。     
已邀请:
        您可以这样做: 首先,在您的HTML中为客户创建一个特定的“克隆模板”,并通过CSS(
.template {display: none;}
)使其不可见。 然后,在您的循环中:
$.each(data.customers, function (key, value) {
    var $newCustomer = $(\'#customers .customer.template\').clone();

    $newCustomer.removeClass(\"template\").find(\'label:first\').text( value.name );
    $(\'#customers\').append( $newCustomer );
});
    

要回复问题请先登录注册